是一个简单的教程,我如何使用WCSessionUserInfoTransfer
在手表和iOS之间更改数据?
必须在iOS -App的代理中写什么?
在我使用的旧编程中:
[WKInterfaceController openParentApplication:@{@"Kommando":@"Radius"} reply:^(NSDictionary *replyInfo, NSError *error) {
if (error) {
NSLog(@"Error from parent: %@", error);
} else {
NSLog(@"Radius from parent: %@", [replyInfo objectForKey:@"Radius"]);
}
}];
答案 0 :(得分:1)
您需要做的第一件事是定义WCSession。必须在您计划向其传输数据和从中接收数据的每个类中定义和激活此项。要使用WCSession
,请确保它受支持,然后激活默认会话,如下所示。
#import <WatchConnectivity/WatchConnectivity.h>
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
从这里开始,您可以在需要发送数据的地方使用transferUserInfo(来自手表或iOS应用程序):
[[WCSession defaultSession] transferUserInfo:@{@"Kommando":@"Radius"}];
在接收端,您将使用session:didReceiveUserInfo
。请注意,与handleWatchKitExtensionRequest
不同,这不需要在iOS应用程序端的应用程序委托中。您可以在需要接收数据的地方使用它。确保在您didReceiveUserInfo
的班级中激活WCSession,如上所示。
- (void)session:(nonnull WCSession *)session didReceiveUserInfo:(nonnull NSDictionary<NSString *,id> *)userInfo {
NSLog(@"Radius from parent: %@", [userInfo objectForKey:@"Radius"]);
}