DavidStrausz/cordova-plugin-today-widget

How to interact with JavaScript?

Ovilia opened this issue · 4 comments

For example, how to know in the App after it was opened by clicking the widget button?
Are there some examples or API to do this? Thanks!

From the official Apple docs:

Opening the Containing App

In some cases, it can make sense for a Today widget to request its containing app to open. For example, the Calendar widget in OS X opens Calendar when users click an event. (Note that in iOS, a user may have to unlock the device before the containing app can open.) To ensure that your containing app opens in a way that makes sense in the context of the user’s current task, you need to define a custom URL scheme that both the app and its widgets can use.

A widget doesn’t directly tell its containing app to open; instead, it uses the openURL:completionHandler: method of NSExtensionContext to tell the system to open its containing app. When a widget uses this method to open a URL, the system validates the request before fulfilling it.

So you will probably need cordova-plugin-customurlscheme to create your own url-scheme, add it to your iOS app and then open your app via openUrl in your widget, then you will be able to send a parameter which tells your app that it was opened by your widget.

Thanks @DavidStrausz ! That's really helpful! ❤️

Another question is, how to update the widget based on information from JavaScript? For example, a TODO list widget should get the list from the App. Would that be too much to ask? 🤦‍♂

No problem, glad to help! :)

Usually you use shared user defaults to exchange (simple) data (your app and your widget are in the same app-group and can therefore access the same user defaults).
Check this article.

You will probably want to use cordova-plugin-nativestorage to save e.g. the TODO list. Especially this section will be of interest for your use-case.

If you need a more advanced data-exchange you could also think about sharing an API-key in the user defaults, so your widget and your app can access the same backend.

@DavidStrausz Thanks so much!