handleWatchKitExtensionRequest未被调用

时间:2015-05-21 08:53:45

标签: ios watchkit

我正在尝试从watchkit app启动父ios应用。我正在使用网址方案来启动应用。但它似乎是

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 

被称为。看起来像手表应用程序确实在背景中启动应用程序。但是,父应用程序不处理watchkit请求。我在一个新项目中尝试了我的方法,它完美无缺。有什么事我需要注意吗?

我已经尝试过调试>附加到进程> myapp并在handleWatchKitExtensionRequest方法中放置一个断点来确认它是否被调用并且它没有被调用。

以下是进度,我在监视应用程序中单击按钮时调用openParentApplication。

@IBAction func viewOniPhoneAction() {


    let userInfo: [NSObject : AnyObject] = [
        "userID" : user.userID
    ]


    WKInterfaceController.openParentApplication(userInfo, reply: { (userInfo : [NSObject : AnyObject]!, error : NSError!) -> Void in

    })

}

这是我的app delegeate

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSDictionary *replyDict = @{@"response": @"done"};
reply(replyDict);
}

我在reply()中尝试了handleWatchKitExtensionRequest,但我在观看应用程序的回复块中收到此错误

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:]}

3 个答案:

答案 0 :(得分:2)

我让它上班!!!有同样的问题......

如果您仍然没有获取数据,只需将beginBackgroundTaskWithExpirationHandler时间增加到更大的值!我之前使用过2秒,但我的网络太弱!!!

我在监视应用程序中单击按钮时调用openParentApplication:

[WKInterfaceController openParentApplication:loadDetailChatDataDictionary reply:^(NSDictionary *replyInfo, NSError *error) {

这是我的应用代表:

- (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
    }

答案 1 :(得分:0)

通过URL方案打开应用程序时,不会调用

handleWatchKitRequest。仅在响应使用openParentApplication:reply:制作的WatchKit扩展中发出的请求时调用它。这就是为什么你没有看到它被执行。

答案 2 :(得分:0)

您需要将回复包装在后台任务中,以确保您的父应用有时间回复。

-

(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo))reply{

    UIApplication *app = [UIApplication sharedApplication];

    UIBackgroundTaskIdentifier bgTask __block = [app beginBackgroundTaskWithName:@"watchAppRequest" expirationHandler:^{

        [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;

    }];

//make your calls here to your tasks, when finished, send the reply then terminate the background task

//send reply back to watch
reply(replyInfo);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [app endBackgroundTask:bgTask];
        bgTask=UIBackgroundTaskInvalid;
    });
}