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

151 views Asked by At

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.

2

There are 2 answers

0
Arasuvel On

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

0
narendrakumar b On

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