我有一个 watch 应用程序需要与父应用程序通信以获取一些信息。这应该只在口袋里使用 watch 和手机时发生。它曾经像这样工作:
在 watch 上的 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
}
我已经看到在 watch 上使用 sendMessage 的示例,但它们都要求委托(delegate)位于打开的手机上的 ViewController 中。有没有办法在手机不使用时从手机上的父应用程序获取信息?
最佳答案
如果这对任何人都有帮助,我发现您必须在主线程上运行它。
- (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);
});
}
关于ios - 如何为 Watch OS 2 更新 openParentApplication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454572/