iOS:在应用程序处于后台时执行上传任务 [英] iOS: Perform upload task while app is in background

查看:350
本文介绍了iOS:在应用程序处于后台时执行上传任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当iOS应用程序在后台运行时,真的没有办法运行UPLOAD任务吗?这是荒唐的.一直在查看诸如NSURLSessionUploadTaskdispatch_after甚至是NSTimer之类的东西,但是在将应用程序置于后台后仅存活了10秒,没有什么比这更有效了.

Is there really no way to run an UPLOAD task while an iOS app is in the background? This is ridiculous. Been looking at various stuff like NSURLSessionUploadTask, dispatch_after and even NSTimer, but nothing works for more than the meager 10 seconds the app lives after being put in the background.

其他具有上传功能的应用程序如何工作?说,将图片上传到Facebook并将应用置于后台,会取消上传吗?

How do other apps that have uploads work? Say, uploading an image to Facebook and putting the app in the background, will that cancel the upload?

为什么iOS无法拥有后台服务或Android和Windows Phone之类的代理?

Why cannot iOS have background services or agents like Android and Windows Phone has?

这是我的应用程序的一项关键功能,在其他平台上也可以正常运行.

This is a critical feature of my app, and on the other platforms is works perfectly.

感谢您的帮助:(

推荐答案

如果您使用

You can continue uploads in the background with iOS 7+ if you use background(withIdentifier:) for the configuration when you instantiate your URLSession.

注意:

  • 您必须使用基于委托的URLSession;

您不能在后台会话中使用任务工厂方法的完成处理程序格式;和

you cannot use the completion handler renditions of the task factory methods with background sessions; and

您还必须使用 uploadTask(with:fromFile:) 方法,而不是Data格式.

您的应用程序委托必须实现 application(_:handleEventsForBackgroundURLSession:completionHandler:) 并捕获该完成处理程序,然后您可以在URLSessionDelegate方法 .

your app delegate must implement application(_:handleEventsForBackgroundURLSession:completionHandler:) and capture that completion handler which you can then call in your URLSessionDelegate method urlSessionDidFinishEvents(forBackgroundURLSession:).

顺便说一句,如果您不想使用背景NSURLSession,但是您希望在应用离开背景后继续运行有限长度的任务超过几秒钟,则可以使用UIApplication方法beginBackgroundTask.这样一来,即使用户离开了该应用程序,您也可以花一点时间(以前是3分钟,在iOS 13及更高版本中只有30秒)完成您正在执行的所有任务.

By the way, if you don't want to use background NSURLSession, but you want to continue running a finite-length task for more than a few seconds after the app leaves background, you can request more time with UIApplication method beginBackgroundTask. That will give you a little time (formerly 3 minutes, only 30 seconds in iOS 13 and later) complete any tasks you are working on even if the user leave the app.

请参见延长应用时间.他们的代码段有些过时了,但是现代的格式可能看起来像这样:

See Extending Your App's Background Execution Time. Their code snippet is a bit out of date, but a contemporary rendition might look like:

var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid

func initiateBackgroundRequest(with data: Data) {
    // Request the task assertion and save the ID.
    backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Finish Network Tasks") {
        // End the task if time expires.
        if self.backgroundTaskID != .invalid {
            UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
            self.backgroundTaskID = .invalid
        }
    }

    // Send the data asynchronously.
    performNetworkRequest(with: data) { result in
        // End the task assertion.
        if self.backgroundTaskID != .invalid {
            UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
            self.backgroundTaskID = .invalid
        }
    }
}

请不要在这里迷失细节.专注于基本模式:

Please don’t get lost in the details here. Focus on the basic pattern:

  • 开始后台任务;
  • 提供一个超时子句,如果您碰巧用完了时间,它将清除后台任务;
  • 启动任何您需要继续进行的操作,即使用户离开了该应用程序也是如此;和
  • 在网络请求的完成处理程序中,结束后台任务.

这篇关于iOS:在应用程序处于后台时执行上传任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆