5

我有一个 iOS 应用程序,我在 appleWatch 上为它编写了一个扩展程序。我正在使用 transferUserInfo 方法将数据(NSDictionary)发送到 appleWatch 扩展。一切都在模拟器中运行,但是当我尝试在真实设备上运行应用程序时,尽管 iPhone 正在发送数据,但 iWatch 似乎没有收到任何内容(我发现这是因为我调试了发送方)。

我已经在两边配置了 WCSession。我已经在我应该接收数据的类中符合 WCSessionDelegate。我正在使用 session:didReceiveUserInfo: 方法在 ExtensionDelegate 中接收数据,但仍然像我说的那样在模拟器中一切正常,但在真实设备上没有传输任何内容。

有没有人知道问题出在哪里?

这是代码:在发送方:

在我的班级 MensaViewController.m

- (void)viewDidLoad
   if ([WCSession isSupported]) {
        session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
   }
   NSLog(@"A dish is being sent");
  [session transferUserInfo:dishDictionary];
}

discDictionary 在 vi​​ewDidLoad 方法中声明,它包含数据。

在接收方(Watch Extension)我配置 WCSession 并在 ExtensionDelegate.m 中接收数据,如下所示:

- (void)applicationDidBecomeActive {
    if ([WCSession isSupported]) {
        session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
        NSLog(@"Session activated in iWatch");
    }
}

我有这种方法来接收数据:

- (void)session:session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo{

    NSLog(@"Received data from the iPhone");

    NSArray<NSString*> *dictionaryKeys = userInfo.allKeys;
    for(int i = 0; i < dictionaryKeys.count; i++){
        Boolean equalsMensaString = [dictionaryKeys[i] isEqualToString:@"beilagen"];
        if(equalsMensaString)
            [self handleTransferredDish:userInfo];

        Boolean equalsNewsString = [dictionaryKeys[i] isEqualToString:@"article"];
        if(equalsNewsString)
            [self handleTransferredNews:userInfo];

        Boolean equalsEventString = [dictionaryKeys[i] isEqualToString:@"description"];
        if(equalsEventString)
            [self handleTransferredEvents:userInfo];

    }
}
4

2 回答 2

2

如果您查看源函数描述,您可以看到用于传输数据的方法的描述:

public func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer

系统会将用户信息字典排入队列并在适当的时候将其传输到对应的应用程序。发送应用程序退出后,用户信息的传输将继续。如果文件成功到达,对应的应用程序将在下次启动时收到委托回调。userInfo 字典只能接受属性列表类型。

所以..无法保证系统会userInfo在您实际使用该应用程序时发送此信息。

请改用以下方法:

public func sendMessage(message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)?, errorHandler: ((NSError) -> Void)?)

客户端可以使用此方法向对应的应用程序发送消息。希望接收对特定消息的回复的客户端应该传入一个replyHandler 块。如果无法发送消息或无法接收回复,则会调用 errorHandler 块并出现错误。如果同时指定了replyHandler 和errorHandler,则将调用其中的一个。只能在发送应用程序运行时发送消息。如果发送应用程序在发送消息之前退出,发送将失败。如果对应应用程序未运行,则对应应用程序将在收到消息后启动(仅限 iOS 对应应用程序)。消息字典只能接受属性列表类型。

并接收:

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)

请注意: 如果对方应用程序未打开或会话未配对且未激活,则此发送可能会失败。所以,如果你不能错过任何你应该使用的数据,updateApplicationContext或者transferUserInfo我实际上更喜欢updateApplicationContext

session.sendMessage(messageData, replyHandler: { (replyData) -> Void in
            replyHandler?(replyData)
            }, errorHandler: { (error) -> Void in
                print("error: code:\(error.code) - \(error.localizedDescription)")
                errorHandler?(error)
                 do {
                        try session.updateApplicationContext(messageData)
                    } catch {
                        print("There was an error trying to update watchkit app on the background")
                    }
        })

并确保您收到这些案例并正确实施

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject])

于 2016-02-08T18:56:54.937 回答
1

我知道我回答自己的问题已经太迟了。我们使用的是 Apple Watch 2 和 iPhone 5 或 6(2 年后不记得了!),发生的事情很神奇,因为 Apple Watch 的电池电量非常低(在 5-10% 范围内),一切都开始工作了。即使在为 Apple Watch 充电后,我们也没有任何问题。结尾!

于 2018-04-12T18:03:45.150 回答