【问题标题】:network connection fails when iPhone is lockediPhone 锁定时网络连接失败
【发布时间】:2014-08-08 07:20:05
【问题描述】:

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

【问题讨论】:

    标签: ios iphone error-handling network-connection


    【解决方案1】:

    你应该看看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;
        });
    }
    

    【讨论】:

    • 这可能不是最好的后台模式。您仅限于几分钟的执行时间。根据 OP 应用,后台获取或 VoIP 模式可能更合适
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2012-03-27
    相关资源
    最近更新 更多