gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:26:46 24 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

您需要移动 session 配置和激活代码

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

发送到 iOS 应用生命周期早期的某个位置,而不是 sendData 消息中。

关于ios - 使用应用程序上下文从 iPhone 向 WatchOS 发送数据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com