我看到this SO张贴在那里显然数据被取出并返回到监视扩展,像这样:WatchKit在handleWatchKitExtensionRequest返回块内回复():
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
if ([[userInfo objectForKey:@"request"] isEqualToString:@"getData"])
{
// get data
// ...
reply(data);
}
}
但是,当我尝试调用“回复( )”获取网络数据,像这样经过块内:
__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];
});
我得到这个错误:
"The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]"
所以我想我必须立即调用reply(),并且在WatchKit唤醒父应用后发送新的网络数据的唯一方法是使用MMWormhole?
我已经包装它在后台任务。我应该清楚地说明并更新了我的问题。我甚至尝试将时间更改为180秒,但仍会立即返回错误。您是否在示例中的“获取数据”中运行了一个块? –
你好,我已经改变了我的答案。通过这种方式,自程序等待dispatch_semaphore_wait(sema,DISPATCH_TIME_FOREVER);直到dispatch_semaphore_signal(sema);被执行后,秒就足够了。它工作吗? – vomako
是的,这工作,谢谢。 –