3
試圖將我的Watch OS1應用程序升級到Watch OS 2.創建Watch OS 2的新目標。並使用sendMessage:replyHandler:errorHandler
發送/從IOS應用程序獲取回覆。它工作正常,如果只有IOS應用程序正在運行。如果Watch應用程序在iOS應用程序處於非活動狀態(Killed狀態)時嘗試通信,則連接錯誤爲7001.如何從Watch App(Watch OS 2)通信非活動的IOS應用程序?如何通過Watch App(Watch OS 2)溝通非活動IOS應用程序
這個來自手錶應用的sendMessage:replyHandler:errorHandler
方法會在後臺喚醒相應的iOS應用並使其可達?
謝謝。
EDIT1 -
iOS應用程序的App代表:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
return YES;
}
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier identifier = UIBackgroundTaskInvalid;
dispatch_block_t endBlock =^{
if (identifier != UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
}
identifier = UIBackgroundTaskInvalid;
};
identifier = [application beginBackgroundTaskWithExpirationHandler:endBlock];
if (replyHandler!=nil) {
replyHandler(resultContainer); // my data dictionary from Iphone app to watch os as reply.
}
if (identifier!=UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
identifier = UIBackgroundTaskInvalid;
}
}
關注應用:
- (void)applicationDidFinishLaunching {
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
NSDictionary *context = @{@"APP_LOADING":@"LOADING"};
[WKInterfaceController reloadRootControllersWithNames:@[WATCH_INTERFACE_LOADING] contexts:@[context]];
NSDictionary *request = //My Request data;
[[WCSession defaultSession] sendMessage:request
replyHandler:^(NSDictionary *reply) {
//handle reply from iPhone app here
NSDictionary *resultDict = [reply objectForKey:WATCH_REQUEST_RESULT];
// Use reply from Phone app
}
errorHandler:^(NSError *error) {
//catch any errors here
// Getting error here 7001 Error.
}
];
}