找到你要的答案

Q:“The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]”

Q:“uiapplicationdelegate在iPhone应用程序从不叫reply()在[ uiapplicationdelegate应用:handlewatchkitextensionrequest:回复:]”

Okay so I'm making this Apple Watch app and in my Watch App, I have a button. When you touch the button, it does this:

[WKInterfaceController openParentApplication:[NSDictionary dictionary] reply:^(NSDictionary *replyInfo, NSError *error) {
    if(error) {
        NSLog(@"%@",error);
    }
}];

And in my App Delegate file I do this:

-(void) application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    //Code that is not importent and is confidential
    reply([NSDictionary dictionary]);
}

Yet I get this error when I press the button:

Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo=0x7fd2b9c35ae0 {NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]}

I'm calling reply() as shown above but it doesn't see that! Also, it's ignoring the other code I have in there!

Can anybody explain why this is happening?

好吧,我做这个苹果手表应用程序和我的手表应用程序,我有一个按钮。当你触摸按钮,它这样做:

[WKInterfaceController openParentApplication:[NSDictionary dictionary] reply:^(NSDictionary *replyInfo, NSError *error) {
    if(error) {
        NSLog(@"%@",error);
    }
}];

在我的应用程序委托文件中,我这样做:

-(void) application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    //Code that is not importent and is confidential
    reply([NSDictionary dictionary]);
}

但是当我按下按钮的时候,我得到了这个错误:

错误代码= 2 = com.apple.watchkit.errors域”uiapplicationdelegate在iPhone应用程序从不叫reply()在[ uiapplicationdelegate应用:handlewatchkitextensionrequest:回复:]“用户信息= 0x7fd2b9c35ae0 { nslocalizeddescription =的uiapplicationdelegate在iPhone应用程序从不叫reply()在[ uiapplicationdelegate应用:handlewatchkitextensionrequest:回复:] }

我打电话reply()上面却没有看到!此外,它忽略了其他代码我有!

有人能解释为什么会这样吗?

answer1: 回答1:

From the documentation:

replyInfo: A dictionary containing data to return to the WatchKit app. The contents of the dictionary must be serializable to a property list file. The contents of this dictionary are at your discretion and you may specify nil.

If you don't send a valid dictionary, you will not get a reply.

从文件:

replyInfo: A dictionary containing data to return to the WatchKit app. The contents of the dictionary must be serializable to a property list file. The contents of this dictionary are at your discretion and you may specify nil.

如果你没有发送有效的字典,你将得不到回复。

answer2: 回答2:

You need to perform all work in handleWatchKitExtensionRequest in a background task. You can find the documentation here: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

This blog post may also prove helpful: http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

你需要完成所有的工作handlewatchkitextensionrequest在后台任务。你可以在这里找到文档:https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/backgroundexecution/backgroundexecution.html

这个博客也可能证明是有用的:http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

answer3: 回答3:

The error "Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" generally means that your app's handleWatchKitExtensionRequest had an error and/or did not call reply() or call it correctly.

The documentation is here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleWatchKitExtensionRequest:reply:

Here is a bit of simple sample code using a NSDictionary response.

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply{
    NSLog(@"containing app received message from watch");
    NSDictionary *response = @{@"response" : @"MyResponse"};
    reply(response);
}

错误“错误域= com.apple.watchkit.errors代码= 2”的uiapplicationdelegate在iPhone应用程序从不叫reply()在[ uiapplicationdelegate应用:handlewatchkitextensionrequest:回复:]”通常意味着你的应用程序的handlewatchkitextensionrequest有错误和/或不叫reply()或正确地调用它。

The documentation is here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleWatchKitExtensionRequest:reply:

这里是一些简单的示例代码使用NSDictionary响应。

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply{
    NSLog(@"containing app received message from watch");
    NSDictionary *response = @{@"response" : @"MyResponse"};
    reply(response);
}
answer4: 回答4:

You may have trouble if your dictionary includes custom classes. Try to send simple strings first and see if that makes a difference.

Check that your "important and confidential" code does not raise exception, or finish without calling a reply (with a error payload, for example).

Your "important and confidential" code must be really quick. If that is not the case, you have to use a background task (see here for examples).

Finally, consider using a background task if your code takes time, does network access. If you use asynchronous network tasks (recommended), reply when you receive the answer, and finish the background task at that time.

Last but not least, the first call to parentApplication fails on some iOS versions : Inconsistent behavior of openParentApplication in my WatchKit App. This solved the last issue for me!

Hope that helps ;-)

如果您的字典包含自定义类,您可能会遇到麻烦。试着先发送简单的字符串,看看是否有差异。

检查您的“重要和机密”代码不引发异常,或不调用应答完成(例如带有错误有效载荷)。

您的“重要和机密”代码必须非常快。如果不是这样的话,你必须使用一个后台任务(见这里的例子)。

最后,考虑使用后台任务,如果您的代码需要时间,网络访问。如果您使用异步网络任务(建议),当您收到答复时回答,并完成当时的后台任务。

最后但并非最不重要的,要用parentApplication第一呼叫失败一些iOS的版本:在我watchkit APP openparentapplication不一致的行为。这为我解决了最后一个问题!

希望有所帮助;-)

ios  objective-c  iphone  watchkit  apple-watch