如何更新Watch OS 2的openParentApplication?

时间:2015-10-31 17:38:34

标签: ios watchkit apple-watch watch-os-2 watch-os

我有一个需要与父应用程序通信的手表应用程序才能获取一些信息。仅在手表和手机放在口袋中时才会发生这种情况。它曾经像这样工作:

在手表上的InterfaceController中:

[InterfaceController openParentApplication:request reply:^(NSDictionary *replyInfo, NSError *error) {
     // handle response from phone
}];

在手机的AppDelegate中:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
    NSDictionary *response = // generate response
    reply(response);
}

我尝试将InterfaceController中的代码更改为:

[[WCSession defaultSession] sendMessage:request
                replyHandler:^(NSDictionary *reply) {
                }
                errorHandler:^(NSError *error) {
                }
];

AppDelegate中的代码似乎永远不会被调用:

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> *replyMessage))replyHandler {
     // this never gets called
}

我已经看过在手表上使用sendMessage的示例,但它们都要求委托在打开的手机上的ViewController中。有没有办法在不使用手机时从手机上的父应用程序获取信息?

1 个答案:

答案 0 :(得分:0)

如果这有助于任何人,我发现你必须在主线程上运行它。

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> *replyMessage))replyHandler {
    dispatch_async(dispatch_get_main_queue(), ^{
        // do stuff here
        replyHandler(replyDictionary);
    });
}