我有一个手表应用程序需要与父应用程序通信以获取一些信息。这应该只在口袋里使用手表和手机时发生。它曾经像这样工作:
在手表上的 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 中。有没有办法在手机不使用时从手机上的父应用程序获取信息?