如果您在手表上使用WCSession
's sendMessage
API,如果 iOS 应用程序尚未运行,它将在后台唤醒它。因此,您可以执行以下操作:
观看扩展代码:
[[WCSession defaultSession] sendMessage:@{@"cmd":@"sendUpdate"} replyHandler:nil errorHandler:^{ /*handle errors*/ }]
iOS 应用代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
<...>
[[WCSession defaultSession] setDelegate:self];
[[WCSession defaultSession] activateSession];
<...>
}
- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error {
}
- (void)sessionDidBecomeInactive:(WCSession *)session
{}
- (void)sessionDidDeactivate:(WCSession *)session
{
[session activateSession];
}
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message {
NSString *cmd = message[@"cmd"];
if ([cmd isEqual:@"sendUpdate"]) {
[self sendUpdate];
}
}
- (void)sendUpdate {
WCSession *session = [WCSession defaultSession];
if (session.isWatchAppInstalled && session.activationState == WCSessionActivationStateActivated) {
[[WCSession defaultSession] transferUserInfo:[self dictionaryFullOfUpdates]];
}
}
这显然比你可能做的简单,但应该给你的想法