【问题标题】:Alamofire upload stops when screen is locked屏幕锁定时 Alamofire 上传停止
【发布时间】:2021-02-12 06:19:46
【问题描述】:

我有一个 iOS 应用程序,它使用 Alamofire 多部分请求将音频文件上传到服务器。上传在前台和后台都可以正常工作,但是当屏幕锁定时,上传会停止并失败。如何在锁屏的情况下继续成功上传文件?

【问题讨论】:

标签: ios swift upload alamofire background-process


【解决方案1】:

您需要使用后台任务上传音频文件link

class ViewController: UIViewController, URLSessionTaskDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let url = URL(string: "http://0.0.0.0")!
        let data = "Secret Message".data(using: .utf8)!
        let request = URLRequest(url: url)
        let config = URLSessionConfiguration.background(withIdentifier: "uniqueId")
        let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
        let task = session.uploadTask(with: request, from: data)
        task.resume()
    }
    
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        print("We're done here")
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多