The UIApplicationDelegate in the iPhone App never called reply

后端 未结 3 517
予麋鹿
予麋鹿 2021-02-13 22:21

I am trying to launch my iPhone app from watch simulator using the below code :

WKInterfaceController subclass

[WKInterfaceController op         


        
3条回答
  •  悲哀的现实
    2021-02-13 22:42

    I would like to add that it is important to start a background task in handleWatchKitExtensionRequest as specified in the documentation. This ensures that the main app on the iPhone is not suspended before it can send its reply. (Not initiating a background task does not cause a problem in the simulator or when the iPhone app is active. However, it causes a problem when the iPhone app is inactive.)

    Code in the app delegate of the main app on iPhone:

    - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
    {
       __block UIBackgroundTaskIdentifier watchKitHandler;
       watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                                   expirationHandler:^{
                                                                     watchKitHandler = UIBackgroundTaskInvalid;
                                                                   }];
    
       if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
       {
          // get data
          // ...
          reply( data );
       }
    
       dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1 ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
           [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
        } );
    }
    

提交回复
热议问题