Trouble sending data from iOS app to companion WatchOS app

I am trying to send data from my iOS app to a companion WatchOS app using WCSession. The iOS app was created with NativeScript, thus the need for Objective-C.


When running the apps on both simulators and real devices I receive the following error message: "[WC] WCSession is missing its delegate"


iOS Objective-C Code (This is being called in Typescript):

#import "SendToWatch.h"
#import 

@interface SendToWatch () 

@end

@implementation SendToWatch

- (void)sendData: (double)value {
    if (WCSession.isSupported) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
   
        NSError *error = nil;
        NSDictionary *applicationDict = @{@"data":[NSString stringWithFormat:@"%0.2f", value]};
        [session updateApplicationContext:applicationDict error:nil];
   
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        }
    }
}

//MARK: - WCSessionDelegate

- (void)session:(WCSession *)session
activationDidCompleteWithState:(WCSessionActivationState)activationState
          error:(NSError *)error {
}

- (void)sessionDidBecomeInactive:(WCSession *)session {
    NSLog(@"Session Did Become Inactive");
}

- (void)sessionDidDeactivate:(WCSession *)session {
    NSLog(@"-- Session Did Deactivate --");
    [session activateSession];
}

@end


WatchOS (InterfaceController.m):

#import "InterfaceController.h"
#import 

@interface InterfaceController () 

@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
   
    // Creates a WCSession to allow iPhone connectivity
    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
        NSLog(@"-- WCSession Active --");
    }
}

- (void)willActivate {
    [super willActivate];
    NSLog(@"-- Controller Activated --");
}

- (void)didDeactivate {
    [super didDeactivate];
    NSLog(@"-- Controller Deactive --");
}

//MARK: - WCSessionDelegate

// Receieves the data sent from the iPhone app
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary *)applicationContext {
    NSString *receivedData = [applicationContext objectForKey:@"data"];

    NSLog(@"-- APPLICATION CONTEXT RECEIVED --");
    NSLog(@"-- Received from iOS App: %@", applicationContext);
   
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.dataLabel setText:receivedData];
        NSLog(@"-- DATA UPDATED --");
    });
}

- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error {
}

@end

Replies

I set the delegate in willActivate. That would mean moving lines 13 to 19 in second part to line 30.


Could that be the reason ?


I also test if session already activated (here in nSwift):

            if session.activationState != .activated {  // activate on the watch side
                session.activate()
            }