|
|
@@ -44,9 +44,16 @@ public class ImageDownloader: NSObject {
|
|
|
}
|
|
|
|
|
|
// MARK: - Public property
|
|
|
+
|
|
|
+ /// This closure will be applied to the image download request before it being sent. You can modify the request for some customizing purpose, like adding auth token to the header or do a url mapping.
|
|
|
+ public var requestModifier: (NSMutableURLRequest -> Void)?
|
|
|
+
|
|
|
/// 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 trustedHosts: Set<String>?
|
|
|
+
|
|
|
// MARK: - Internal property
|
|
|
let barrierQueue = dispatch_queue_create(downloaderBarrierName, DISPATCH_QUEUE_CONCURRENT)
|
|
|
let processQueue = dispatch_queue_create(imageProcessQueueName, DISPATCH_QUEUE_CONCURRENT)
|
|
|
@@ -108,6 +115,9 @@ public extension ImageDownloader {
|
|
|
let timeout = self.downloadTimeout == 0.0 ? 15.0 : self.downloadTimeout
|
|
|
let request = NSMutableURLRequest(URL: URL, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: timeout)
|
|
|
request.HTTPShouldUsePipelining = true
|
|
|
+
|
|
|
+ self.requestModifier?(request)
|
|
|
+
|
|
|
let task = session.dataTaskWithRequest(request)
|
|
|
|
|
|
task.priority = options.lowPriority ? NSURLSessionTaskPriorityLow : NSURLSessionTaskPriorityDefault
|
|
|
@@ -183,33 +193,46 @@ extension ImageDownloader: NSURLSessionDataDelegate {
|
|
|
|
|
|
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
|
|
|
|
|
|
- let URL = task.originalRequest.URL!
|
|
|
-
|
|
|
- if let error = error { // Error happened
|
|
|
- callbackWithImage(nil, error: error, imageURL: URL)
|
|
|
- } else { //Download finished without error
|
|
|
-
|
|
|
- // We are on main queue when receiving this.
|
|
|
- dispatch_async(processQueue, { () -> Void in
|
|
|
+ if let URL = task.originalRequest.URL {
|
|
|
+ if let error = error { // Error happened
|
|
|
+ callbackWithImage(nil, error: error, imageURL: URL)
|
|
|
+ } else { //Download finished without error
|
|
|
|
|
|
- if let fetchLoad = self.fetchLoads[URL] {
|
|
|
- if let image = UIImage(data: fetchLoad.responseData) {
|
|
|
- if fetchLoad.shouldDecode {
|
|
|
- self.callbackWithImage(image.kf_decodedImage(), error: nil, imageURL: URL)
|
|
|
+ // We are on main queue when receiving this.
|
|
|
+ dispatch_async(processQueue, { () -> Void in
|
|
|
+
|
|
|
+ if let fetchLoad = self.fetchLoads[URL] {
|
|
|
+ if let image = UIImage(data: fetchLoad.responseData) {
|
|
|
+ if fetchLoad.shouldDecode {
|
|
|
+ self.callbackWithImage(image.kf_decodedImage(), error: nil, imageURL: URL)
|
|
|
+ } else {
|
|
|
+ self.callbackWithImage(image, error: nil, imageURL: URL)
|
|
|
+ }
|
|
|
+
|
|
|
} else {
|
|
|
- self.callbackWithImage(image, error: nil, imageURL: URL)
|
|
|
+ self.callbackWithImage(nil, error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.BadData.rawValue, userInfo: nil), imageURL: URL)
|
|
|
}
|
|
|
-
|
|
|
} else {
|
|
|
self.callbackWithImage(nil, error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.BadData.rawValue, userInfo: nil), imageURL: URL)
|
|
|
}
|
|
|
- } else {
|
|
|
- self.callbackWithImage(nil, error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.BadData.rawValue, userInfo: nil), imageURL: URL)
|
|
|
- }
|
|
|
-
|
|
|
- self.cleanForURL(URL)
|
|
|
- })
|
|
|
+
|
|
|
+ self.cleanForURL(URL)
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
|
|
|
+
|
|
|
+ if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
|
|
|
+ if let trustedHosts = trustedHosts where trustedHosts.contains(challenge.protectionSpace.host) {
|
|
|
+ let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
|
|
|
+ completionHandler(.UseCredential, credential)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ completionHandler(.PerformDefaultHandling, nil)
|
|
|
+ }
|
|
|
+
|
|
|
}
|