iOS / Apple Watch:当应用程序处于后台时,不会触发iPhone应用程序网络请求回调块

时间:2015-05-30 18:15:00

标签: ios objective-c afnetworking watchkit apple-watch

我的Apple Watch应用程序向随播iPhone应用程序发送消息。在主应用程序handleWatchKitExtensionRequest中,我向服务器发送请求:

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    if ([[userInfo objectForKey:@"request"] isEqualToString:@"getPendingChallenge"]) {
        [MyClient getPendingNotifications:someId withDomain:host withSuccessBlock:^(id responseObject) {
            // process responseObject
            ...
            reply(response);
            return;
        } withFailureBlock:^(NSError *error, NSString *responseString) {
            // error handling
            return;
        }];
    }
}
上面的

getPendingNotifications只是使用AFNetworking的常规网络GET请求。

当应用程序处于活动状态时,一切正常。由于此网络请求用于填充Apple Watch上的UI,因此我不希望主应用程序处于活动状态。但是,当iPhone上的主应用程序处于后台时,我可以看到网络请求被发送出去,但上述代码中的withSuccessBlockwithFailureBlock回调块永远不会被触发。

手机应用可以在后台模式下接收网络请求响应吗?如果是这样,我做错了什么?

2 个答案:

答案 0 :(得分:2)

我找到了一个适合我的在线解决方案,Brian Gilham的帖子(http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply)。

这是适用于我的代码。

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    // There is a chance that the iOS app gets killed if it's in the background
    // before it has a chance to reply to Apple Watch.
    // The solution is to have the app respond to the request asap, then complete other tasks.
    // The following code begins – and ends, after two seconds – an empty background task right at the beginning of this delegate method
    // Then we kick off a background task for the real work
    // For more details see http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

    __block UIBackgroundTaskIdentifier bogusWorkaroundTask;
    bogusWorkaroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] endBackgroundTask:bogusWorkaroundTask];
    });

    __block UIBackgroundTaskIdentifier realBackgroundTask;
    realBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        reply(nil);
        [[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask];
    }];

    if ([[userInfo objectForKey:@"request"] isEqualToString:@"getPendingChallenge"]) {
        [self handleWatchKitGetPendingChallengeRequest:reply];
    }

    [[UIApplication sharedApplication] endBackgroundTask:realBackgroundTask];
}

- (void)handleWatchKitGetPendingChallengeRequest:(void (^)(NSDictionary *))reply {
    ...
    [MyClient getPendingNotifications:someId withDomain:host withSuccessBlock:^(id responseObject) {
        // process responseObject
        reply(response);
        return;
    } withFailureBlock:^(NSError *error, NSString *responseString) {
        // error handling
        reply(nil);
        return;
    }];
}

答案 1 :(得分:0)

尝试将请求作为同步请求发送。

我猜你的请求是异步请求(因为它应该在常规情况下)。在后台模式下,设备将在后台线程中为您的应用程序提供午餐,并为请求创建了新线程。