在 Xamarin 中使用 watchOS 父应用程序Working with the watchOS Parent Application in Xamarin

重要

使用以下示例访问父应用程序仅适用于 watchOS 1 监视应用。Accessing the parent application using the examples below only works on watchOS 1 watch apps.

可以通过不同的方式在监视应用和它所捆绑到的 iOS 应用之间进行通信:There are different ways to communicate between the watch app and the iOS app that it is bundled with:

  • 监视扩展可以针对在 iPhone 上后台运行的父应用调用方法Watch extensions can call a method against the parent app that runs in the background on the iPhone.

  • 监视扩展可以与父 iPhone 应用共享存储位置Watch extensions can share a storage location with the parent iPhone app.

  • 使用移交将数据从速览或通知传递到 Watch 应用,并将用户发送到应用中的特定界面控制器。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

GpsWatch 示例演示了监视扩展和父 iPhone 应用之间的通信。Communicating between a watch extension and the parent iPhone app is demonstrated in the GpsWatch sample. 你的监视扩展可以请求父 iOS 应用使用 OpenParentApplication 方法代表其执行某种处理。Your watch extension can request the parent iOS app to do some processing on its behalf using the OpenParentApplication method.

这对于长时间运行的任务(包括网络请求)尤其有用-只有父 iOS 应用才能利用后台处理来完成这些任务,并将检索到的数据保存到可访问监视扩展的位置。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

在手表应用扩展中调用 WKInterfaceController.OpenParentApplicationCall the WKInterfaceController.OpenParentApplication in your watch app extension. 它将返回一个指示是否已成功发送方法请求的 boolIt returns a bool that indicates whether the method request was sent successfully or not. 检查响应 Action 中的 error 参数,确定在 iPhone 应用中运行的方法期间是否发生了任何错误。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 应用iOS App

来自监视应用扩展的所有调用都通过 iPhone 应用的 HandleWatchKitExtensionRequest 方法进行路由。All calls from a watch app extension are routed through the iPhone app's HandleWatchKitExtensionRequest method. 如果在手表应用中发出不同的请求,则此方法将需要查询 userInfo 字典,以确定如何处理请求。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

如果配置应用组,则 iOS 8 扩展(包括监视扩展)可以与父应用共享数据。If you configure an app group then iOS 8 extensions (including watch extensions) can share data with the parent app.

使用 nsuserdefaultsNSUserDefaults

可以在 "监视应用扩展" 和 "父 iPhone" 应用中编写以下代码,以便它们可以引用一组常见的 NSUserDefaultsThe 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

IOS 应用和监视扩展还可以使用公共文件路径共享文件。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

注意:如果路径为 null,请检查应用组配置,以确保正确配置了预配配置文件并将其下载/安装到了开发计算机上。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.

WormHoleSharpWormHoleSharp

一种常用的开源机制,适用于 watchOS 1 (基于MMWormHole)在父应用程序和监视应用程序之间传递数据或命令。A popular open-source mechanism for watchOS 1 (based on MMWormHole) to pass data or commands between the parent app and the watch app.

可以在 iOS 应用和监视扩展中使用类似于下面的应用组配置旋涡式星体: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");

下载C#版本WormHoleSharpDownload the C# version WormHoleSharp.