【发布时间】:2015-05-09 06:57:06
【问题描述】:
我看到this SO 的帖子显然正在获取数据并返回到 Watch 扩展程序,如下所示:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}
}
但是当我在获取网络数据后尝试在块内调用'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 App 中的 UIApplicationDelegate 从未在 -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:] 中调用 reply()”
所以我想我必须立即调用 reply() 并且在 WatchKit 唤醒父应用程序后发送新网络数据的唯一方法是使用 MMWormhole?
【问题讨论】:
标签: ios objective-c watchkit apple-watch