iPhone被锁定后,网络连接失败



我开发了一个应用程序,当服务器响应时,如果iPhone被锁定,则会发出http请求(异步调用)。网络连接失败,我得到一个错误代码-1005"网络连接失败"。如何在手机被锁定的情况下保持网络连接,并在手机再次解锁或在后台允许app接收响应?当用户在收到响应时杀死应用程序时,应用程序也会崩溃。

你应该看看后台执行和多任务处理。它允许你的应用程序在后台运行长达10分钟。

下面是文档中的示例代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do the work associated with the task, preferably in chunks.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

最新更新