Search code examples
iosobjective-cwatchkitwatchos-2

Apple Watch OS 2: Is there a to transfer data from iPhone to Watch App without Watch app being in foreground?


I have a Watch App that needs to have data from the iPhone App. I transfer it like so.

    if ([WCSession isSupported]) {
       WCSession *session = [WCSession defaultSession];
       session.delegate = self;
       [session activateSession];
       }

     if ([[WCSession defaultSession] isReachable]) {
          NSArray *keys = [NSArray arrayWithObjects: @"data", @"data1" ,nil];
          NSArray *objects = [NSArray arrayWithObjects:data,data1, nil];
          NSDictionary *applicationDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
         [[WCSession defaultSession] sendMessage:applicationDict replyHandler:^(NSDictionary *replyHandler) {

  } errorHandler:^(NSError *error) {

    }];
  }

and receive it like so.

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

}

However, this only works if the Apple Watch is in the foreground. Is there a way around this where Apple Watch App can receive the data without the App being in the foreground or maybe there is an alternative way of doing this like waking up the Apple Watch App before sending the data.


Solution

  • What you can do is to use the updateApplicationContext:error: method on WCSession object to send updated data to your watch. When your watch app wakes up, it will receive the context object with the updated data.