iOS Tutorial [Tool] iWidget - add a today widget to your App

JanPRO

Well-Known Member
Licensed User
iWidget is a (experimental) tool which allows you to add a today widget to your B4i app.
widget.PNG
widget.jpg

How to use?
  1. Download iWidget here
  2. Close the B4i IDE
  3. You need to fill in the following information
    • the path of the B4i installation directory
    • the path of the B4i project you want to add the widget to
    • the name of your App (corresponds to the #ApplicationLabel)
    • the package name of your App (corresponds to the Package under the Build Configurations)
    • a title for the widget
    • choose a port for the logger (see the explanation of the logger in the notes section)
  4. Click on the Apply button
  5. Open the B4i project
  6. Add the attached modules (TodayWidget & WidgetContext) to your project
  7. if you use the logger don't forget to set Logger_Host & Logger_Port variables
  8. Start coding your widget :)

Notes & tips:
  • start with the attached HelloWorld project, for the first time it's possible that you have to add the widget manually by clicking on the circled edit button
  • use a wildcard certificate for your project
  • the minimum required iOS version is 10, so add: #MinVersion: 10 to your project attributes
  • always keep in mind the consequences of the fact, that a widget is a totally new target. For example the widget can't access values of a variable, which were set in the main app
  • the PerformUpdate event is invoked when the widget is loaded and it can also be called in the background. In this sub you need to check whether there is new data available and update your UI. From my experience you should do the same routine also in the Widget_Appear sub
  • I strongly recommend to create the UI with the designer and use anchors
  • the normal Log method will be useless for widgets. Instead make use of the Wlog function in the TodayWidget module
  • the logger will not catch errors: If your widget returns "Unable to load" there is a mistake in your code, use try-catch blocks to locate the issue. However, if you have a local mac, you can open the generated Xcode project and run the Widget target specifically (in this case you will be able to see the logs added with the normal Log method and also see errors)
  • when working on a widget always test in release mode
  • everytime you activate or deactivate iWidget you have to restart B4i
  • when working on your widget project, keep iWidget open
  • administrator rights are required for editing the Xcode template

  • iWidget was tested with B4i v.4.81 + local mac builder (Xcode 9.2), I don't know whether the hosted builder has restrictions concerning the template file, just try it out ;)

Feel free to ask questions ;)
 

Attachments

Last edited:

walterf25

Expert
Licensed User
iWidget is a (experimental) tool which allows you to add a today widget to your B4i app.

How to use?
  1. Download iWidget here
  2. Close the B4i IDE
  3. You need to fill in the following information
    • the path of the B4i installation directory
    • the path of the B4i project you want to add the widget to
    • the name of your App (corresponds to the #ApplicationLabel)
    • the package name of your App (corresponds to the Package under the Build Configurations)
    • a title for the widget
    • choose a port for the logger (see the explanation of the logger in the notes section)
  4. Click on the Apply button
  5. Open the B4i project
  6. Add the attached modules (TodayWidget & WidgetContext) to your project
  7. if you use the logger don't forget to set Logger_Host & Logger_Port variables
  8. Start coding your widget :)

Notes & tips:
  • start with the attached HelloWorld project, for the first time it's possible that you have to add the widget manually by clicking on the circled edit button
  • use a wildcard certificate for your project
  • the minimum required iOS version is 10, so add: #MinVersion: 10 to your project attributes
  • always keep in mind the consequences of the fact, that a widget is a totally new target. For example the widget can't access values of a variable, which were set in the main app
  • the PerformUpdate event is invoked when the widget is loaded and it can also be called in the background. In this sub you need to check whether there is new data available and update your UI. From my experience you should do the same routine also in the Widget_Appear sub
  • I strongly recommend to create the UI with the designer and use anchors
  • the normal Log method will be useless for widgets. Instead make use of the Wlog function in the TodayWidget module
  • the logger will not catch errors: If your widget returns "Unable to load" there is a mistake in your code, use try-catch blocks to locate the issue. However, if you have a local mac, you can open the generated Xcode project and run the Widget target specifically (in this case you will be able to see the logs added with the normal Log method and also see errors)
  • when working on a widget always test in release mode
  • everytime you activate or deactivate iWidget you have to restart B4i
  • when working on your widget project, keep iWidget open
  • administrator rights are required for editing the Xcode template

  • iWidget was tested with B4i v.4.81 + local mac builder (Xcode 9.2), I don't know whether the hosted builder has restrictions concerning the template file, just try it out ;)

Feel free to ask questions ;)
This is really cool, just tried and it's so easy to set it up, one question though, how does the Update work, I see that in your example the time gets updated every minute, but how is that controlled, can it be set up to update every so often, or how do I control that?

Thanks for this, I will add it to one of my apps right away, but will wait for your reply on the Update question..
Walter
 

JanPRO

Well-Known Member
Licensed User
Hi Walter,
first of all, sorry for the super late reply, but it's kind of busy around here. I am glad, you like this project :)
but how is that controlled, can it be set up to update every so often, or how do I control that?
The answer is simple: the update frequency is controlled by Apple, you don't have influence on it. Also take a look at the following post: https://stackoverflow.com/a/31306820
I recommend to always update your UI in the PerformUpdate and Widget_Appear sub, because the appear event is called every time the widget is displayed. Moreover, if you have time critical content you should add a small refresh button/icon to your UI.

You are adding very critical tools for us for b4i. Thanks very much !!
Thank you very much Tufan. I know that these kind of tools (see also "Swifter") may look impractical at first glance, however they demonstrate the potential of B4i.

Have a great 2019

Jan
 

ShaneG30

Well-Known Member
Licensed User
Is there any possible way to see the source code of the desktop application? It would be pretty neat to see how it works.
 
Top