我正在开发适用于iOS的小型跟踪应用。用户可以创建任意数量的活动,然后跟踪他在每个活动上花费的时间(例如,睡觉,开车上班,吃饭,工作等)。
现在我想添加Apple Watch支持:用户应该能够从他手表上的列表中选择活动,输入时间值并将此结果提交给iOS应用程序,然后iOS应用程序更新其数据。
所以,我需要双向沟通:
iOS -> Watch:
List of configured activties
Watch -> iOS
Add time XY for activity 123
我在双方都实施了WCSession
,但无论我尝试什么,似乎都没有沟通:
// iOS App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
if ([WCSession isSupported]) {
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)updateActitviesOnWatch:(NSDictionary<NSString *, id> *)data {
if (session != nil && session.paired && session.activationState == WCSessionActivationStateActivated) {
[session updateApplicationContext:data error:nil];
[session sendMessage:data replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
NSLog(@"sendMessage : success");
} errorHandler:^(NSError * _Nonnull error) {
NSLog(@"sendMessage : error");
}];
[session transferUserInfo:data];
}
}
// WatchExtension ExtensionDelegate.m
- (void)applicationDidFinishLaunching {
if ([WCSession isSupported]) {
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error {
NSLog(@"WK activationDidCompleteWithState");
}
- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *, id> *)userInfo {
NSLog(@"WK session didReceiveUserInfo");
}
- (void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *, id> *)applicationContext {
NSLog(@"WK session didReceiveApplicationContext");
}
- (void)session:(WCSession * __nonnull)session didFinishUserInfoTransfer:(WCSessionUserInfoTransfer *)userInfoTransfer error:(nullable NSError *)error {
NSLog(@"WK session didFinishUserInfoTransfer");
}
无论使用哪种方法在iOS上发送数据(updateApplicationContext:error:
,sendMessage:replyHandler:errorHandler
或transferUserInfo:
),都会在Watch上调用非接收委托方法。
我做错了什么?
首先传输数据的正确方法是什么?