我开发了一个应用程序,当iPhone被锁定时,当服务器响应时发出http请求(异步调用)。网络连接失败,我收到错误代码-1005“网络连接失败”。如何在手机锁定时保持网络连接,并允许应用在手机再次解锁或在后台解锁时接收响应。当用户在收到回复时杀死应用程序时,应用程序也会崩溃。
答案 0 :(得分:2)
你应该看看Background Execution and Multitasking。它允许您的应用在后台运行大约10分钟。
以下是doc:
中的示例代码- (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;
});
}