ios - iPhone 锁定时网络连接失败

标签 ios iphone error-handling network-connection

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

最佳答案

您应该看看Background Execution and Multitasking 。它允许您的应用在后台运行长达约 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;
    });
}

关于ios - iPhone 锁定时网络连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197992/

相关文章:

iphone - 查找父 UIActionSheet

ios - 在 iOS 上,是否需要执行 CGContextRelease?

ios - Quickblox iOS - 请求对象,按 'Created At' 排序

iphone - 推荐用于iPhone开发的线程层?

python - 在 Python 中检查图像 URL 是否指向真实图像

java - 定义错误代码,数字或字符串?

error-handling - “This XML file does not appear to have any style information”

iphone - NSMutableArray removeObjectAtIndex : Behaviour

ios - NSUserDefault 在后退按钮上快速同步

iphone - 如何通过键盘添加 UIView - iOS