无法将命令从 Apple Watch 发送到 iPhone,但命令正在从 iPhone 发送到 Apple Watch

Unable to send commands from Apple Watch to iPhone but commands are going from iPhone to Apple Watch

我正在开发示例 Apple Watch 应用程序并尝试从 Apple Watch 向 iPhone 发送命令。 收到以下错误消息。

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

从 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)会导致错误。

我还不能发表评论,所以... 您可能希望在调用 sendMessage 时将 self.session 替换为 [WCSession defaultSession]。有时 ARC 会出于任何原因释放该 var。

这是我的代码,对我有用:

 [[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,因此您可以在手表上跳过它。

如果有帮助请告诉我,如果没有帮助我会继续更新我的答案。

我猜这是 watch 的一个错误 OS 2 https://forums.developer.apple.com/thread/19213