ios - iPhone 锁定时网络连接失败
<p><p>我开发了一个应用程序,当 iPhone 被锁定时服务器响应时会发出一个 http 请求(异步调用)。网络连接失败,我收到错误代码 -1005“网络连接失败”。如何在手机锁定时保持网络连接,并允许应用在手机再次解锁或在后台接收响应。当用户在收到响应时杀死应用程序时,应用程序也会崩溃。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你应该看看 <a href="https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html" rel="noreferrer noopener nofollow">Background Execution and Multitasking</a> .它允许您的应用在后台运行大约 10 分钟。</p>
<p>以下是文档中的示例代码:</p>
<pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
;
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.
;
bgTask = UIBackgroundTaskInvalid;
});
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - iPhone 锁定时网络连接失败,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25197992/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25197992/
</a>
</p>
页:
[1]