gpt4 book ai didi

ios - 我的 WatchKit 应用程序中 openParentApplication 的行为不一致

转载 作者:可可西里 更新时间:2023-11-01 03:13:05 26 4
gpt4 key购买 nike

我正在开发一个 Apple Watch 应用程序,它使用 openParentApplication:reply: 方法与其父应用程序通信。

父应用程序与 Web 服务通信,并通过使用包含数据的 NSDictionary 调用 reply 方法将它获得的数据发送回 watch 扩展。

当父应用程序在前台或后台打开时,该应用程序可以完美运行。但是如果我打开父应用程序然后使用任务切换器终止它, watch 扩展第一次调用 openParentApplication:replyInfo: 时,它会收到以下错误和参数 replyInfo 为 nil。

iPhone App 中的 UIApplicationDelegate 从未调用过 reply()

但是每次 openParentApplication:replyInfo: 调用扩展后都会得到正确的响应。

我检查并发现 watch 扩展程序第一次调用时,handleWatchKitExtensionRequest:reply: 永远不会在父应用程序上调用。

这可能是什么原因?

我按照文档中的建议,在后台任务中执行 handleWatchKitExtensionRequest:reply: 中的所有操作。这是我的一些代码:来 self 的扩展程序的代码:

NSDictionary *params = @{@"requestCode": @(RequestGetLoggedIn)};

[WKInterfaceController openParentApplication:params reply:^(NSDictionary *replyInfo, NSError *error) {
// Do something with the result
}];

来自父应用的代码:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
}];

NSNumber* requestCode = userInfo[@"requestCode"];

// Perform some request and then call reply()

// End the background task
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
});
}

编辑 1:问题同时出现在模拟器和真实的 Apple Watch 上。

最佳答案

看来 iOS 8.4 中存在错误。

我在 application:didFinishLaunchingWithOptions:handleWatchKitExtensionRequest:reply: 的开头添加了 NSLog,执行了导致的操作问题,然后检查设备日志并得到这个:

--- Notice>: (Warn ) WatchKit: <SPCompanionAppServer.m __91-[SPCompanionAppServer launchCompanionAppForGizmoAppWithIdentifier:withUserInfoData:reply:]_block_invoke_2:1450> Got BSActionErrorCodeResponseNotPossible for com.xyz.xyz.watchkitapp. This will translate to WatchKitApplicationDelegateWatchKitRequestReplyNotCalledError

... Irrelevant stuff

--- WatchKit Extension[1686] <Warning>: __59-[InformationController getNotificationListIncremental:]_block_invoke (null)
**--- <Warning>: MY LOG: Application did launch with parameters (null)**

此日志显示 application:didFinishLaunchingWithOptions: 在操作系统给出有关未从父应用程序获得响应的错误后被调用。如果应用程序没有首先启动,它将如何做出响应?

我暂时解决了这个问题,在出现这个问题的时候再次调用openParentApplication:reply:方法

我实现重试一次行为的方法是创建一个包装调用的方法并使用该方法代替原始方法。我将其作为类方法添加到实用程序类中,但它也可以是全局函数。

+ (void)openParentApplication:(NSDictionary*)params reply:(void(^)(NSDictionary *replyInfo, NSError *error))reply
{
[WKInterfaceController openParentApplication:params reply:^(NSDictionary *replyInfo, NSError *error) {
if (error.domain == WatchKitErrorDomain && error.code == WatchKitApplicationDelegateWatchKitRequestReplyNotCalledError)
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[WKInterfaceController openParentApplication:params reply:^(NSDictionary *replyInfo, NSError *error) {
reply(replyInfo, error);
}];
});
}
else
reply(replyInfo, error);
}];
}

关于ios - 我的 WatchKit 应用程序中 openParentApplication 的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210092/

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