WatchKit在handleWatchKitExtensionRequest块中返回reply():

我看到这个 SOpost显然是数据被提取并返回到Watch扩展,如下所示:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply { if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] ) { // get data // ... reply( data ); } } 

但是当我尝试在获取networking数据之后调用“reply()”内部块时,

 __block UIBackgroundTaskIdentifier watchKitHandler; watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask" expirationHandler:^{ watchKitHandler = UIBackgroundTaskInvalid; }]; - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply { NSMutableDictionary *response = [NSMutableDictionary dictionary]; [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){ if (succeeded) { [response setObject:@"update succeded" forKey:@"updateKey"]; reply(response); } else { if (error) { [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; reply(response); } else { [response setObject:@"update failed with no error" forKey:@"updateKey"]; reply(response); } } }]; } dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 180), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler]; }); 

我得到这个错误:

“iPhone应用程序中的UIApplicationDelegate从来没有在 – [UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]中调用reply()

所以我想我必须立即调用reply(),在WatchKit唤醒父应用程序之后发送新的networking数据的唯一方法是使用MMWormhole?

为了确保您的asynchronous数据获取不会立即返回(没有调用回复),您可以尝试以下操作:

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply { __block UIBackgroundTaskIdentifier watchKitHandler; watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask" expirationHandler:^{ watchKitHandler = UIBackgroundTaskInvalid; }]; NSMutableDictionary *response = [NSMutableDictionary dictionary]; dispatch_semaphore_t sema = dispatch_semaphore_create(0); [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){ if (succeeded) { [response setObject:@"update succeded" forKey:@"updateKey"]; reply(response); } else { if (error) { [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; dispatch_semaphore_signal(sema); reply(response); } else { [response setObject:@"update failed with no error" forKey:@"updateKey"]; dispatch_semaphore_signal(sema); reply(response); } } }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 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]; }); } 

始终参考官方文件。 正式文件

以下代码适用于我。

 __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ // Clean up any unfinished task business by marking where you // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task, preferably in chunks. [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){ if (succeeded) { [response setObject:@"update succeded" forKey:@"updateKey"]; reply(response); } else { if (error) { [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; reply(response); } else { [response setObject:@"update failed with no error" forKey:@"updateKey"]; reply(response); } } }]; [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); 

我想你把你的代码弄混了。 你需要将beginBackgroundTaskWithName移动到你的handleWatchKitExtensionRequest让它成为第一件事,然后调用你的函数。

简单的例子:

 -(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo))reply{ UIApplication *app = [UIApplication sharedApplication]; UIBackgroundTaskIdentifier bgTask __block = [app beginBackgroundTaskWithName:@"watchAppRequest" expirationHandler:^{ NSLog(@"Background handler called. Background tasks expirationHandler called."); [[UIApplication sharedApplication] endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //do your background task(s) here if([requestString isEqualToString:@“myRequest"]){ //send reply back to watch reply(); } //end background task [app endBackgroundTask:bgTask]; bgTask=UIBackgroundTaskInvalid; }