使用应用程序上下文将数据从 iPhone 发送到 WatchOS 时遇到问题

Trouble sending data from iPhone to WatchOS using Application Context

我正在尝试使用 WCSession 将数据从我的 iOS 应用程序发送到配套的 WatchOS 应用程序。 iOS 应用程序是使用 NativeScript 创建的,因此需要 Objective-C。

当 运行 在模拟器和真实设备上运行应用程序时,我收到以下错误消息:

[WC] WCSession is missing its delegate

这几天我一直在纠结这个问题,但无法解决这个问题。

iOS Objective-C 代码(在 Typescript 中被调用):

#import "SendToWatch.h"
#import <WatchConnectivity/WatchConnectivity.h>

@interface SendToWatch () <WCSessionDelegate>

@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 <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController () <WCSessionDelegate>

@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

您需要移动会话配置和激活码

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

到 iOS 应用程序生命周期早期的某个地方,而不是 sendData 消息中。

为了解决这个问题,我不得不使用 tns-platform-declarations 插件在打字稿中重写 Objective-C 代码。 WCSession 现在有一个委托并正在向我的配套 WatchOS 应用程序发送数据。