Search code examples
ios8ios8-extensionios8-today-widget

Requesting data from a Today Extension


Seems like WatchKit is built using the same extension mechanism brought forth in ios8.

I started into making a Today Extension and read through the guide. It says data is shared using App Groups, as is the case in WatchKit, but, unlike it WK, it seems to be saying that you should:

  • not assume the parent app is running
  • wake it up to get data

So my question is simple: am I wrong about this? I want some information from the app. It seems like I could grab the context object and call openURL and ask the app to refresh the data in the channel then have my extension listen for the updated notification. But I can't find any examples that do that.


Solution

  • That's probably not what you want in this case. You can use the extension context to open a URL that's handled by the containing app. But when you do, the parent app will come to the foreground and your today extension won't be visible. That's great if you want to switch to the app, but it's not good if you just want to get some data while keeping the extension visible.

    The usual approach is not to ask the containing app for data, but rather to store the data in a location where both the app and the extension can read it directly. That's the point of using app groups-- you save your data in the group directory, and the extension reads what it needs. This doesn't require that the containing app is running, and it also doesn't launch the app.

    Once app groups are configured, you find the location using -[NSFileManager containerURLForSecurityApplicationGroupIdentifier:]. Put your data there, and it's available to any app or extension with the proper group capability.