Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sessionReachabilityDidChange not called on watch

I would like to have my watch app respond to the parent app on the phone being killed. When the watch app is running and the phone app is killed I get no callback from either sessionReachabilityDidChange or sessionWatchStateDidChange. Based on apple documentation:

This method is called to let the current process know that its counterpart session’s reachability changed.

So, it seems that I should get a callback. I've set the WCSession delegate to my class on the watch. The session on the watch receives callbacks for application context. Why am i not getting a reachability callback?

Code Below..

+ (SomeClass *)sharedInstance {
    static dispatch_once_t pred;
    static SomeClass *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[SomeClass alloc] init];
        [Model sharedInstance].delegate = shared;
    });
    return shared;
}

#pragma mark - setup

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

-(BOOL) hasValidWCSession {
    return ([WCSession isSupported] && [WCSession defaultSession].isReachable);
}


#pragma mark - WCSessionDelegate

- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *, id> *)applicationContext {

    NSLog(@"application context received on watch: %@", applicationContext);
    [[Model sharedInstance] process:applicationContext];
}


- (void)sessionWatchStateDidChange:(WCSession *)session {
    NSLog(@"wcession state changed on watch");
}

- (void)sessionReachabilityDidChange:(WCSession *)session {
    NSLog(@"wcsession reachability changed on watch");
}
like image 809
Bryan Boyko Avatar asked Mar 02 '16 22:03

Bryan Boyko


1 Answers

The iPhone app counterpart does not need to be running in order for reachable to be true from the perspective of the Apple Watch app. If the iPhone itself is paired and reachable, sending messages using WCSession from the watch app will launch the iPhone app in the background.

Thus, I would not expect sessionReachabilityDidChange: to be called on the watch app when the iOS app is killed. This also means that your iPhone app should be prepared to be launched in the background at any time, and activate WCSession promptly, to handle incoming requests from the watch app (similar to how certain push notifications, etc., can launch an iOS app).

However, from the perspective of the iPhone app, the watch app counterpart is only considered reachable when "a paired and active Apple Watch is in range and the associated Watch app is running in the foreground" (documentation).

Also, note that sessionWatchStateDidChange: is invoked when "the value in the paired, watchAppInstalled, complicationEnabled, or watchDirectoryURL properties of the WCSession object changes".

like image 55
Mike Mertsock Avatar answered Oct 24 '22 00:10

Mike Mertsock