我正在尝试从 watchkit 应用程序启动父 ios 应用程序。我正在使用 url scheme 来启动应用程序。但它看起来像
-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
从未被调用。似乎 watch 应用程序确实在后台启动了该应用程序。但是父应用程序不处理 watchkit 请求。我在一个新项目中尝试了我的方法,并且效果很好。有什么需要注意的吗?
我已经尝试调试>附加到进程>myapp 并在 handleWatchKitExtensionRequest 方法中放置一个断点以确认它是否被调用和未被调用。
这是进度,在 watch 应用中单击按钮时我调用 openParentApplication。
@IBAction func viewOniPhoneAction() {
let userInfo: [NSObject : AnyObject] = [
"userID" : user.userID
]
WKInterfaceController.openParentApplication(userInfo, reply: { (userInfo : [NSObject : AnyObject]!, error : NSError!) -> Void in
})
}
这是我的应用委托(delegate)
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSDictionary *replyDict = @{@"response": @"done"};
reply(replyDict);
}
我在 handleWatchKitExtensionRequest
中尝试了 reply()
但我在 watch 应用程序的回复 block 中遇到了这个错误
Error Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo=0x60800026e0c0 {NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]}
最佳答案
我成功了!!!有同样的问题....
如果仍然没有得到数据,只需将 beginBackgroundTaskWithExpirationHandler 时间增加到一个更大的值!!!我之前用了 2 秒,但是我的网络太弱了!!!
我在 watch 应用程序中单击按钮时调用 openParentApplication:
[WKInterfaceController openParentApplication:loadDetailChatDataDictionary reply:^(NSDictionary *replyInfo, NSError *error) {
这是我的应用委托(delegate):
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
__block UIBackgroundTaskIdentifier bogusWorkaroundTask;
bogusWorkaroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // increase the time to a larger value if you still don't get the data!!! I used 2 secs previously but my network is too weak!!!
[[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
});
// --------------------
__block UIBackgroundTaskIdentifier realBackgroundTask;
realBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
reply(nil);
[[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask];
}];
NSString *value = userInfo[@"key"];
if ([value isEqualToString:@"loadRecentChatData"]) {
reply(@{@"recents":recents}); // Add your reply here
}
关于ios - 未调用 handleWatchKitExtensionRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368896/