Jaa


Working with the watchOS Parent Application in Xamarin

There are different ways to communicate between the watch app and the iOS app that it is bundled with:

  • Watch apps can run code on the parent app on the iPhone.

  • Watch extensions can share a storage location with the parent iPhone app.

  • Use handoff to pass data from a notification to the watch app, sending the user to a specific interface controller in the app.

The Parent App is also sometimes referred to as the Container App.

Shared Storage

If you configure an app group then iOS 8 extensions (including watch extensions) can share data with the parent app.

NSUserDefaults

The following code can be written in both the watch app extension and the parent iPhone app so that they can reference a common set of NSUserDefaults:

NSUserDefaults shared = new NSUserDefaults(
        "group.com.your-company.watchstuff",
        NSUserDefaultsType.SuiteName);

// set values
shared.SetInt (2, "count");
shared.Synchronize ();

// get values
shared.Synchronize ();
var count = shared.IntForKey ("count");

Files

The iOS app and watch extension can also share files using a common file path.

var FileManager = new NSFileManager ();
var appGroupContainer =
            FileManager.GetContainerUrl ("group.com.your-company.watchstuff");
var appGroupContainerPath = appGroupContainer.Path;
Console.WriteLine ("agcpath: " + appGroupContainerPath);
// use the path to create and update files

Note: if the path is null then check the app group configuration to ensure the provisioning profiles have been configured correctly and have been downloaded/installed on the development computer.

For more information, please see the App Group Capabilities documentation.