Communicate Data Between Watchos & Today Extension Widget

Communicate data between WatchOS & Today Extension widget

At the moment there is no way to achieve this using any built-in frameworks. Since the introduction of watchOS2, WatchKit apps are considered to be independent apps and not just an extension of the iOS app, hence you cannot use AppGroups to share data between the two and especially cannot use it to share data between the WatchKit app and an iOS extension.

As you already experienced, WatchConnectivity cannot be used in a Today extension, so that is out of the picture as well.

Your only option is a suboptimal one, since you will need to upload your data to a server from your watchOS app and download it in your iOS Today extension. This of course generates unnecessary data usage, but with the current frameworks provided by Apple you cannot share the data offline.

However, if you wait until watchOS4 is released (or give the beta version a try), you might be able to leverage the CoreBluetooth framework, which is becoming available for WatchKit in watchOS4 to communicate between a WatchKit app and an iOS extension offline, using BLE. I am not 100% sure if CoreBluetooth can be used in iOS Extensions, but you should give this a try if you can use watchOS4 beta.

WCSession and Today Widget

If the watch app is running in the foreground, you should be able to send it a message via a Darwin notification. Take a look at MMWormhole.

Share data between main app and today widget macOS

To share data between app/app extension in macOS project :

1/ In your Main project target, select Capabilities and enable "App Groups".

2/ Click on "+" and add a new app group name, it must start with your team ID (e.g. 643J438K.name.app)

3/ Repeat 1/ and 2/ for your extension app target, be sure to add the same name

4/ To save and fetch data use :

// Store data
UserDefaults(suiteName: "643J438K.name.app")!.set(yourVariable, forKey: "yourKey")
UserDefaults(suiteName: "643J438K.name.app")!.synchronize()

// Fetch data (integer type here)
UserDefaults(suiteName: "643J438K.name.app")!.integer(forKey: "yourKey")

5/ You can check that your data is correctly saved by opening this file :

/Users/UserName/Library/Group Containers/643J438K.name.app/Preferences/643J438K.name.app.plist

Today's Extension communicate with Main App

You can't communicate directly with your main app as you want. You can basically do two things:

  • Open your main app from your extension
  • Pass data through your shared container


Related Topics



Leave a reply



Submit