Sfoglia il codice sorgente

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

Noon, Christian 11 anni fa
parent
commit
582267b974
1 ha cambiato i file con 9 aggiunte e 0 eliminazioni
  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.
     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.
     */
     required public init(configuration: NSURLSessionConfiguration? = nil) {
         self.delegate = SessionDelegate()
         self.session = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
+
+        self.delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
+            if let strongSelf = self {
+                strongSelf.backgroundCompletionHandler?()
+            }
+        }
     }
 
     // MARK: -