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

Inconsistent behavior of openParentApplication in my WatchKit App

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

父应用程序与 Web 服务通信,并通过调用 reply 方法和包含数据的 NSDictionary 将其获取的数据发送回手表扩展。

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

UIApplicationDelegate in the iPhone App never called reply()

但是此后分机发出的每个 openParentApplication:replyInfo: 调用都会得到正确的响应。

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

这可能是什么原因?

我按照文档中的建议在后台任务中执行 handleWatchKitExtensionRequest:reply: 中的所有操作。这是我的一些代码: 来自我的分机的代码:

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.

中似乎存在错误

我已将 NSLog 添加到 application:didFinishLaunchingWithOptions:handleWatchKitExtensionRequest:reply: 的开头,执行导致问题的操作,然后检查设备日志并得到以下信息:

--- 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: 在 OS 给出有关未从父应用程序获得响应的错误后被调用。如果应用程序没有首先启动,它将如何做出响应?

遇到这个问题我又调用了openParentApplication:reply:方法暂时解决了

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

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

我遇到了同样的问题。请参阅我为向 Apple 提交错误报告而制作的以下基本项目:https://www.dropbox.com/s/ayltpprjck37ins/HandleWatchkitExtensionError%202.zip?dl=0

@Cihan Tek - 你怎么打电话给openParentApplication:reply:又来了?您是在回复块中调用它吗?我这样做时仍然收到错误消息。