从 Apple Watch 向设备发送消息时出现以下错误
Error Domain=WCErrorDomain Code=7012 "Message reply took too long." UserInfo={NSLocalizedDescription=Message reply took too long., NSLocalizedFailureReason=Reply timeout occured.}
#import <WatchConnectivity/WatchConnectivity.h>
在 watch 和主要应用程序目标中,并且符合 watch 和设备上的委托(delegate)方法
从 watch 向设备发送消息
- session 确认为可用
确认 session 可达
NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:@[@"SomethingHere"] forKeys:@[@"valueKey"]]; if([[WCSession defaultSession] isReachable]) { NSLog(@"Reachable"); //<---- Console shows reachable [[WCSession defaultSession] sendMessage:applicationDict replyHandler:^(NSDictionary *reply) { NSLog(@"%@",reply); } errorHandler:^(NSError *error) { NSLog(@"%@",error); //<--- returns error }]; }
设备
在 appdelegate didFinishLaunching
// Watch kit session
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(@"\n\n - WatchKit Session Started - \n\n");
}
else{
NSLog(@"WatchKit Session Error");
}
session 确认如期开始
在设备上接收消息
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {
NSLog(@"Data delagte");
dispatch_async(dispatch_get_main_queue(), ^{
resultFromWatch = [message objectForKey:@"resultDataValue"];
});
}
更新:
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)message {
dispatch_async(dispatch_get_main_queue(), ^{
});
}
停止根据 ccjensen 评论收到的错误消息
最佳答案
检查这些东西:
1/确保在手机端正确实现 WCSessionDelegate。 (不知道你到目前为止是否和/或实现了多少)
特别是,确保你实现了 session(_:didReceiveMessage:replyHandler:).
2/确保您确实调用了 WCSessionDelegate 中的 replyHandler,如文档中突出显示的那样:“您必须执行回复 block 作为实现的一部分。” WCSessionDelegate Protocol Reference
3/检查完这些后,确保在重新尝试使用 watch 之前运行最新版本的 iPhone 应用程序。
如果这些不起作用,则意味着您的 WCSessionDelegate 实现速度太慢,因此超时,或者您在 watch 和手机之间遇到了一个很好的老式网络问题(在 sim 中不太可能,但可能存在错误)。
希望这对您有所帮助。
编辑: 我没有提到,计数器部分应用程序必须处于事件状态才能响应。这意味着,iPhone 应用程序必须至少在后台(启动一次)才能响应。 如果不是,一段时间后您将超时。
关于ios - 错误 : "Message reply took too long" sending message to device Watch kit OS 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32279740/