AlamoFire在后台会话中下载

时间:2015-05-09 00:21:29

标签: ios swift network-programming alamofire

我在新应用中使用Alamofire(基于Alamofire的下载管理器示例) 我需要一些关于使用后台会话下载文件的说明。 我需要覆盖SessionDelegate才能使它工作? 或者只是backgroundCompletionHandler

通常使用Alamofire处理后台下载的步骤是什么? 我如何处理我的应用程序相关的情况,下载量不断变化。

3 个答案:

答案 0 :(得分:31)

更新

基于this amazing tutorial,我在GitHub上提供了一个示例项目。它有一个后台会话管理的例子。

根据Apple的URL Loading System Programming Guide

  

在iOS和OS X中,当用户重新启动您的应用时,您的应用   应该立即创建后台配置对象   与您的任何未完成任务的会话相同的标识符   应用程序上次运行,然后为每个应用程序创建一个会话   配置对象。这些新会话同样是自动的   与正在进行的背景活动重新相关。

显然,通过使用适当的后台会话配置实例,您的下载永远不会在#flux;#34;。

我还发现this answer非常有帮助。

原始答案

来自Alamofire' GitHub page

  

应用程序可以为后台和短暂创建管理器   会话,以及自定义默认会话的新管理器   配置,例如默认标头(HTTPAdditionalHeaders)或   超时间隔(timeoutIntervalForRequest)。

默认情况下,顶级方法使用具有默认会话配置的共享Manager实例。但是,您可以创建一个具有后台会话配置的管理器,如下所示:

let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.example.app.background")
let manager = Alamofire.Manager(configuration: configuration)

然后,您可以使用此Manager实例发出请求。

manager.startRequestsImmediately = true
let request = NSURLRequest(URL: NSURL(string: "your.url.here")!)
manager.request(request)

通过查看其实现,它还有一个名为backgroundCompletionHandler的属性,因此您可以添加完成块:

manager.backgroundCompletionHandler = {
        // do something when the request has finished
    }

答案 1 :(得分:25)

Alamofire实际上非常容易:

1)您的Alamofire.Manager应配置后台会话标识符:

class NetworkManager {
    ...
    private lazy var backgroundManager: Alamofire.SessionManager = {
        let bundleIdentifier = ...
        return Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: bundleIdentifier + ".background"))
    }()
    ...
}

2)在App Delegate实现application(_:handleEventsForBackgroundURLSession:completionHandler:中并将完成处理程序传递给Alamofire.SessionManager.backgroundCompletionHandler

在我的情况下,app委托方法看起来像

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    NetworkManager.default.backgroundCompletionHandler = completionHandler
}

我的网络管理员有一个像这样的计算属性来设置Manager属性:

var backgroundCompletionHandler: (() -> Void)? {
    get {
        return backgroundManager.backgroundCompletionHandler
    }
    set {
        backgroundManager.backgroundCompletionHandler = newValue
    }
}

答案 2 :(得分:7)

我一直在寻找解决方案。直到阅读上面提到的文章。对我来说问题是 - 我必须启用外部附件通信

enter image description here

其他一切都如上所述完成。 的AppDelegate:

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        BackendAPIManager.sharedInstance.backgroundCompletionHandler = completionHandler
    }

的Singleton:

import Alamofire

class BackendAPIManager: NSObject {
    static let sharedInstance = BackendAPIManager()

    var alamoFireManager : Alamofire.SessionManager!

    var backgroundCompletionHandler: (() -> Void)? {
        get {
            return alamoFireManager?.backgroundCompletionHandler
        }
        set {
            alamoFireManager?.backgroundCompletionHandler = newValue
        }
    }

    fileprivate override init()
    {
        let configuration = URLSessionConfiguration.background(withIdentifier: "com.url.background")
        configuration.timeoutIntervalForRequest = 200 // seconds
        configuration.timeoutIntervalForResource = 200
        self.alamoFireManager = Alamofire.SessionManager(configuration: configuration)
    }
}

调用按以下方式完成:

BackendAPIManager.sharedInstance.alamoFireManager.upload(multipartFormData: { (multipartFormData) in ...