情况:我在Watch应用中使用openParentApplication
来呼叫主应用中的handleWatchKitExtensionRequest
。这在模拟器中运行良好,当iPhone应用程序处于活动/打开状态时,它也适用于实际设备(Apple Watch和iPhone)。
问题:当我在实际设备(Apple Watch和iPhone)上运行时,handleWatchKitExtensionRequest
在主iPhone应用未激活时不会将数据返回openParentApplication
/打开。
WatchKit扩展中的InterfaceController.m中的代码:
NSDictionary *requst = @{ @"request" : @"getData" };
[InterfaceController openParentApplication:requst
reply:^( NSDictionary *replyInfo, NSError *error ) {
// do something with the returned info
}];
iPhone主应用程序的app委托中的代码:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}
}
答案 0 :(得分:16)
当iPhone上的主应用程序未激活时,可能无法访问reply()
,因为之前操作系统已将后台任务杀死。
解决方案是在handleWatchKitExtensionRequest
中明确启动- (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];
} );
}
中的后台任务。如果后台任务是这样启动的,它最多可以运行180秒。这可以确保iPhone上的主应用程序在发送回复之前不会被暂停。
iPhone主应用程序的app委托中的代码:
- (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"];
}
else
{
if (error)
{
[response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"];
}
else
{
[response setObject:@"update failed with no error" forKey:@"updateKey"];
}
}
reply(response);
dispatch_semaphore_signal(sema);
}];
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];
});
}
如果您需要异步获取数据,请使用以下方法确保该方法不会立即返回(不调用回复):
function AddEventListeners() {
document.getElementById('txtHangman').addEventListener('keypress', TextHangman.bind(this), false);
}