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

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

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

当父应用程序在前台或后台打开时,该应用程序可以正常工作。 但是,如果我打开父应用程序,然后使用任务切换器来终止它,则手表扩展名首次调用openParentApplication:replyInfo: ,会得到以下错误,并且参数replyInfo以nilforms出现。

UIApplicationDelegate in the iPhone App never called reply()

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

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

这可能是什么原因?

我正在执行handleWatchKitExtensionRequest:reply:中的所有操作handleWatchKitExtensionRequest:reply:在后台任务中,如文档中所build议的。 这是我的一些代码:从我的扩展代码:

 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中有一个错误。

我已经添加NSLogapplication:didFinishLaunchingWithOptions:开始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:被调用后操作系统提供了一个关于没有得到父应用程序的响应的错误。 如果应用程序没有首先启动,应用程序如何做出回应?

我已经暂时解决了这个问题,当这个问题发生时再次调用openParentApplication:reply:方法。

一次执行重试的方式是通过创build一个方法来封装调用,并使用该方法代替原始方法。 我把它作为一个类方法添加到了一个工具类中,但它也可以是一个全局函数。

 + (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:again? 你在回复块中调用它吗? 当我这样做时,我仍然收到错误。