WCSession: Data submitted on iOS is never received on the Watch

202 Views Asked by At

I am working on a small time tracking app for iOS. The user can create any number of activity and then track how much time he spend on each of them (e.g. sleeping, driving to work, eating, working, etc.)

Now I would like to add Apple Watch support: The user should be able to pick an activity from a list on his watch, enter a time value and submit this result to the iOS app which then updates its data.

So, I need communication in both directions:

iOS -> Watch:
    List of configured activties

Watch -> iOS
    Add time XY for activity 123

I implemented the WCSession on both sides but no matter what I try, there seems to be no communication:

// iOS App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    ...
    if ([WCSession isSupported]) {
        session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }
}

- (void)updateActitviesOnWatch:(NSDictionary<NSString *, id> *)data {
    if (session != nil && session.paired && session.activationState == WCSessionActivationStateActivated) {
        [session updateApplicationContext:data error:nil];

        [session sendMessage:data replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
            NSLog(@"sendMessage : success");
        } errorHandler:^(NSError * _Nonnull error) {
            NSLog(@"sendMessage : error");
        }];

        [session transferUserInfo:data];
    }
}


// WatchExtension ExtensionDelegate.m
- (void)applicationDidFinishLaunching {
    if ([WCSession isSupported]) {
        session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }
}

- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error {
    NSLog(@"WK activationDidCompleteWithState");
}

- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *, id> *)userInfo {    
    NSLog(@"WK session didReceiveUserInfo");
}

- (void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *, id> *)applicationContext {
    NSLog(@"WK session didReceiveApplicationContext");
}

- (void)session:(WCSession * __nonnull)session didFinishUserInfoTransfer:(WCSessionUserInfoTransfer *)userInfoTransfer error:(nullable NSError *)error {
    NSLog(@"WK session didFinishUserInfoTransfer");
}

No matter which method is used to send the data on iOS (updateApplicationContext:error:, sendMessage:replyHandler:errorHandler or transferUserInfo:) non of the receiving delegate methods is called on the Watch.

What ma I doing wrong?

What is the correct method to transferee the data in the first place?

0

There are 0 best solutions below