简体   繁体   中英

How can we send a message from iphone to AppleWatch when AppleWatch in backgroundstate or sleepmode?

I am working on sample watch kit app (Apple Watch + iPhone). Here my app is syncing fine. And also I worked on WCSession using data transferring (iPhone to apple watch). It is working fine when both are in active state. But I couldn't transfer data to Applewatch if it is inactive state (background state, sleep mode or foreground state). delegate method not calling.

- (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];
}
}

Here is my delegate method.

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

Please help me.

If iOS device and Apple watch are paired. Then when sending message through using WCSession, it will automatically wake the app.

By using the follwing code i solved my problem.

In 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 {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM