gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:24 25 4
gpt4 key购买 nike

我开发了一个应用程序,当 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com