0

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

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

[WC] WCSession 缺少它的代表

我已经搞砸了几天,但无法解决这个问题。

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(接口控制器.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
4

2 回答 2

0

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

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

在 iOS 应用程序生命周期的早期,而不是在sendData消息中。

于 2019-09-27T05:12:39.990 回答
0

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

于 2019-10-30T11:38:58.993 回答