2016-04-29 61 views
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. 
            } 
     ]; 

} 

回答

0

由於激活方法是異步的使用是在委託方法類似如下:

public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?){ 
    print("activationDidCompleteWith") 
    if activationState == WCSessionActivationState.activated { 
     NSLog("Activated") 
     if(WCSession.default().isReachable){ 

      do { 
       try session.updateApplicationContext(
        [WatchRequestKey : "updateData"] 
       ) 
      } 
      catch let error as NSError { 
       print("\(error.localizedDescription)") 
      } 
     } 
    } 

    if activationState == WCSessionActivationState.inactive { 
     NSLog("Inactive") 
    } 

    if activationState == WCSessionActivationState.notActivated { 
     NSLog("NotActivated") 
    } 
} 
相關問題