Working with the Parent Application

PDF for offline use
Related Articles:
Related Samples:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2016-09

Sharing data between the iOS and watch app in watchOS 1

NOTE: Accessing the parent application using the examples below only works on watchOS 1 watch apps.

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

  • Watch extensions can call a method against the parent app that runs in the background on the iPhone.

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

  • Using Handoff to pass data from a Glance or 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.

Run Code

Communicating between a watch extension and the parent iPhone app is demonstrated in the GpsWatch sample. Your watch extension can request the parent iOS app to do some processing on its behalf using the OpenParentApplication method.

This is especially useful for long running tasks (including network requests) - only the parent iOS app can take advantage of background processing to complete these tasks and save the retrieved data in a location accessible to the watch extension.

Watch Kit App Extension

Call the WKInterfaceController.OpenParentApplication in your watch app extension. It returns a bool that indicates whether the method request was sent successfully or not. Check the error parameter in the response Action to determine if any occurred during the method running in the iPhone app.

WKInterfaceController.OpenParentApplication (new NSDictionary (), (replyInfo, error) => {
    if(error != null) {
        Console.WriteLine (error);
        return;
    }
    Console.WriteLine ("parent app responded");
    // do something with replyInfo[] dictionary
});

iOS App

All calls from a watch app extension are routed through the iPhone app's HandleWatchKitExtensionRequest method. If you are making different requests in the watch app then this method will need to query the userInfo dictionary to determine how to process the request.

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    // ... other AppDelegate methods
    public override void HandleWatchKitExtensionRequest
        (UIApplication application, NSDictionary userInfo, Action<NSDictionary> reply)
    {
        var status = 2;
        // do something in the background, and respond
        reply (new NSDictionary (
            "count", NSNumber.FromInt32 ((int)status),
            "value2", new NSString("some-info")
            ));
    }
}

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 Working with App Groups section of our Device Provisioning documentation.

WormHoleSharp

A popular open-source mechanism for watchOS 1 (based on MMWormHole) to pass data or commands between the parent app and the watch app.

You can configure WormHole using an app group like this in your iOS app and watch extension:

// AppDelegate (iOS) or InterfaceController (watch extension)
Wormhole wormHole;
// ...
wormHole = new Wormhole ("group.com.your-company.watchstuff", "messageDir");

Download the C# version WormHoleSharp.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.