UIApplication.shared.beginBackgroundTask在iOS 13上不起作用

问题描述 投票:5回答:1

UIApplication.shared.beginBackgroundTask在iOS 13上不起作用。是否有其他选择可在iOS 13上实现长时间运行的后台任务?另外,它在iOS 12及以下版本上也能完美运行。

[当应用程序进入后台时,它会在2分钟内终止,而我希望它在后台继续运行数小时,因为我们正在后台进行一些处理。

下面是我正在使用的代码,因此当我的应用程序在后台运行时,它可以节省额外的执行时间。我们需要额外的执行时间,因为我们会定期将当前位置发送到服务器。

/// Register background task. This method requests additional background execution time for the app
private func registerBackgroundTask() {
    DispatchQueue.global().async {
        self.backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "BgTask", expirationHandler: {
            // Ends long-running background task
            self.endBackgroundTask()
        })
    }
}

/// Ends long-running background task. Called when app comes to foreground from background
private func endBackgroundTask() {
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
}
ios13 swift4.2
1个回答
0
投票

工作正常。根据定义,它应将应用程序的后台执行时间延长几分钟,以便您完成已经开始的任务,如果未正确完成,则可能对您的应用程序有害。过去是10分钟,现在是3分钟。实际上,根据我在网上找到的最新文章来看,我认为这可以减少到30秒

因此,这绝对不是满足您需求的正确工具。尝试查看URSessionConfiguration.background(withIdentifier:),但要小心,因为它周围存在很多风险和错误。

此外,请在互联网上搜索Dropbox的背景位置更新,以了解Dropbox如何克服背景限制。

祝你好运

© www.soinside.com 2019 - 2024. All rights reserved.