当AppleWatch在backgroundstate或sleepmode中时,我们如何从iphone向AppleWatch发送消息?

时间:2016-12-19 13:29:54

标签: ios objective-c iphone apple-watch wcsession

我正在研究样品手表套件应用程序(Apple Watch + iPhone)。在这里,我的应用程序正在同步。此外,我还使用数据传输(iPhone到Apple手表)处理WCSession。两者都处于活动状态时工作正常。但如果数据处于非活动状态(背景状态,睡眠模式或前景状态),我无法将数据传输到Applewatch。委托方法不调用。

- (void)applicationDidBecomeActive {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if ([WCSession isSupported]) {
    WCSession *session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];
}
}

这是我的委托方法。

 - (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

如果iOS设备和Apple手表配对。然后,当使用WCSession发送消息时,它将自动唤醒应用程序。

答案 1 :(得分:0)

通过使用以下代码我解决了我的问题。

在Viewcontroller中:

// Sending data to iphone to applewatch
WCSession *session = [WCSession defaultSession];
    NSError *error;
    [session updateApplicationContext:@{@"counterValue":@"counter", @"userId":@"id"} error:&error];

Applewatch :( Interfacecontroller)

//Receiving data in applewatch from iphone
- (void) session:(nonnull WCSession* )session didReceiveApplicationContext:(nonnull NSDictionary<NSString * ,id> *)applicationContext {
}