Przeglądaj źródła

Merge pull request #21 from onevcat/feature/challenge

Add challenge handler for image downloader
Wei Wang 10 lat temu
rodzic
commit
97db633fcf
1 zmienionych plików z 16 dodań i 0 usunięć
  1. 16 0
      Kingfisher/ImageDownloader.swift

+ 16 - 0
Kingfisher/ImageDownloader.swift

@@ -47,6 +47,9 @@ public class ImageDownloader: NSObject {
     /// The duration before the download is timeout. Default is 15 seconds.
     public var downloadTimeout: NSTimeInterval = 15.0
     
+    /// A set of trusted hosts when receiving server trust challenges. A challenge with host name contained in this set will be ignored. You can use this set to specify the self-signed site.
+    public var trustHosts: Set<String>?
+    
     // MARK: - Internal property
     let barrierQueue = dispatch_queue_create(downloaderBarrierName, DISPATCH_QUEUE_CONCURRENT)
     let processQueue = dispatch_queue_create(imageProcessQueueName, DISPATCH_QUEUE_CONCURRENT)
@@ -212,4 +215,17 @@ extension ImageDownloader: NSURLSessionDataDelegate {
         }
     }
 
+    public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
+
+        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
+            if let trustHosts = trustHosts where trustHosts.contains(challenge.protectionSpace.host) {
+                let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
+                completionHandler(.UseCredential, credential)
+                return
+            }
+        }
+        
+        completionHandler(.PerformDefaultHandling, nil)
+    }
+    
 }