gpt4 book ai didi

ios - 如何正确使用 "openParentApplication"和 "handleWatchKitExtensionRequest"以便调用 "reply()"?

转载 作者:技术小花猫 更新时间:2023-10-29 10:16:16 25 4
gpt4 key购买 nike

情况:我在 Watch 应用程序中使用 openParentApplication 来调用主应用程序中的 handleWatchKitExtensionRequest。这在模拟器中运行良好,并且在 iPhone 应用程序处于事件/打开状态时也适用于实际设备(Apple Watch 和 iPhone)。

问题: 当我在实际设备(Apple Watch 和 iPhone)上运行它时,handleWatchKitExtensionRequest 没有返回数据给 openParentApplication主 iPhone 应用程序未激活/打开。

WatchKit 扩展中 InterfaceController.m 中的代码:

NSDictionary *requst = @{ @"request" : @"getData" };
[InterfaceController openParentApplication:requst
reply:^( NSDictionary *replyInfo, NSError *error ) {
// do something with the returned info
}];

iPhone 上主应用的应用委托(delegate)中的代码:

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

最佳答案

当 iPhone 上的主应用程序未激活时,reply() 可能无法到达,因为后台任务之前已被操作系统杀死。

解决方案是在 handleWatchKitExtensionRequest 中显式启动后台任务,如 documentation 中指定的那样.如果像这样启动后台任务,它最多可以运行 180 秒。这可确保 iPhone 上的主应用程序在发送回复之前不会暂停。

iPhone 上主应用的应用委托(delegate)中的代码:

- (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];
} );
}

如果您需要异步获取数据,请使用以下方法确保该方法不会立即返回(不调用 reply):

- (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];
});
}

关于ios - 如何正确使用 "openParentApplication"和 "handleWatchKitExtensionRequest"以便调用 "reply()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30000274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com