2

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.

Rob
  • 11,446
  • 7
  • 39
  • 57

1 Answers1

1

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Yeah I understand the point of App Groups, have done a watch app. The problem with them is that I don't have any way for the containing app to know that I want that data updated NOW. That's what I was trying to point out in the question. So sure I can and am using data that is shared there. It's just I need some data from the containing app on demand, that is guaranteed to be updated. – Rob May 18 '15 at 23:06
  • Thanks for the clarification about the app coming to the foreground with the openURL call. For sure rules this out. – Rob May 18 '15 at 23:06
  • You need notifications between the app and the extension. A quick way to get that is using [MMWormhole](https://github.com/mutualmobile/MMWormhole). For other options, see [my blog post on the topic](http://www.atomicbird.com/blog/sharing-with-app-extensions). – Tom Harrington May 18 '15 at 23:08
  • but you don't receive update with mmwormhole when app is in background.... – altagir Oct 18 '16 at 15:43