无法从Apple Watch发送命令到iPhone,但可以从iPhone发送到Apple Watch。

3
我正在开发一个样例的Apple Watch应用程序,并试图从Apple Watch向iPhone发送命令。但是出现了以下错误信息:

2015-12-21 17:44:17.942 SampleWatch WatchKit Extension[157:4572] requestRecord error: Error Domain=WCErrorDomain Code=7001 "未知的WatchConnectivity错误。" UserInfo={NSUnderlyingError=0x17d51350 {Error Domain=com.apple.identityservices.error Code=23 "超时" UserInfo={NSUnderlyingError=0x17d4a550 {Error Domain=com.apple.ids.idssenderrordomain Code=12 "(null)"}, NSLocalizedDescription=超时}}, NSLocalizedDescription=未知的WatchConnectivity错误。}

以下是从Watch向iPhone发送命令的代码片段:
    - (void)sendCommand
    {
       NSLog(@" ### SendCommand :%d ###",[self.session isReachable] );
       if ([self.session isReachable]) {
       NSDictionary *message = @{@"Command": @"Hello"};
       [self.session sendMessage:message
                 replyHandler:^(NSDictionary *reply) {
                     //handle reply from iPhone app here
                     NSLog(@"requestRecord reply: %@", reply);
                 }
                 errorHandler:^(NSError *error) {
                     //catch any errors here
                     NSLog(@"requestRecord error: %@", error);
                 }
         ];
    }
}

按照以下方式设置会话:

    - (void)setupWatchSession
    {
        if ([WCSession isSupported]) {

        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];

        NSLog(@"@setupWatchSession: %@", self.session);
      }
    }

同样的,即使是在iPhone上也可以进行设定。其中一种方式(从iPhone到Apple Watch)可以接收指令,但另一种方式(从Apple Watch到iPhone)会出现错误。

在iOS应用程序代码中,您在哪里执行WCSession的设置(appdelegate、viewcontroller的viewDidLoad等)? - ccjensen
类似的方法也被写入了AppDelegate中以在iOS中设置WCSession。 - Dantuluri
通常我在iOS应用程序没有激活其WCSession时看到这个错误,这就是为什么我问的原因。你能否发布iOS端didReceiveMessage的实现? - ccjensen
2个回答

1
我还不能评论,所以... 当你调用sendMessage时,你可能想要将self.session替换为[WCSession defaultSession]。有时候ARC会因为某种原因dealloc该变量。
以下是我的代码,它对我有效:
 [[WCSession defaultSession] sendMessage:@{@"file":urlOut.absoluteString, @"langFrom":langFrom,@"langTo":langTo, @"data":dataFromFile } replyHandler:^(NSDictionary * _Nonnull replyMessage) {
                NSLog(@"%@",replyMessage);
[[HistoryDemon summon] addItem:replyMessage];
[self presentControllerWithName:@"TranslationTableView" context:nil];
} errorHandler:^(NSError * _Nonnull error) { NSLog(@"%@",error); }];
注意:我仍在使用watchOS 2.0 //太害怕升级到2.1 :D
还有一件事,从watchOS 2.0开始,[WCSession isSupported]始终为YES,所以你可以在手表上跳过它。
如果这有帮助,请告诉我,如果没有,我会不断更新我的答案。

尝试将整个代码从InterfaceController类移动到ExtensionDelegate中,但问题仍然存在。 - Dantuluri
嗯,好的,你试过这个了吗: [self.session updateApplicationContext:applicationDict error:nil]; 发送它并捕获它使用 - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message; - 4oby
你可以尝试将这些设备“解除配对”,然后重新启动手机和手表,再将它们“作为新设备”进行配对。有时这样能有所帮助。 - 4oby
嘿,老兄,非常感谢。它起作用了。取消配对并将其重新配对为新设备已经起作用 :) :) :) - Dantuluri
我也遇到了同样的问题。重启iPhone和Apple Watch! - Kotik_o
显示剩余3条评论

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接