浏览代码

Added a backgroundCompletionHandler and call it when the session finishes background events.

Noon, Christian 11 年之前
父节点
当前提交
582267b974
共有 1 个文件被更改,包括 9 次插入0 次删除
  1. 9 0
      Source/Alamofire.swift

+ 9 - 0
Source/Alamofire.swift

@@ -291,12 +291,21 @@ public class Manager {
     /// Whether to start requests immediately after being constructed. `true` by default.
     /// Whether to start requests immediately after being constructed. `true` by default.
     public var startRequestsImmediately: Bool = true
     public var startRequestsImmediately: Bool = true
 
 
+    /// The background completion handler closure provided by the UIApplicationDelegate `application:handleEventsForBackgroundURLSession:completionHandler:` method. By setting the background completion handler, the SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` closure implementation will automatically call the handler. If you need to handle your own events before the handler is called, then you need to override the SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` and manually call the handler when finished. `nil` by default.
+    public var backgroundCompletionHandler: (() -> Void)?
+
     /**
     /**
         :param: configuration The configuration used to construct the managed session.
         :param: configuration The configuration used to construct the managed session.
     */
     */
     required public init(configuration: NSURLSessionConfiguration? = nil) {
     required public init(configuration: NSURLSessionConfiguration? = nil) {
         self.delegate = SessionDelegate()
         self.delegate = SessionDelegate()
         self.session = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
         self.session = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
+
+        self.delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
+            if let strongSelf = self {
+                strongSelf.backgroundCompletionHandler?()
+            }
+        }
     }
     }
 
 
     // MARK: -
     // MARK: -