Created
March 20, 2017 19:40
-
-
Save aerickson14/e9ee1172519025c82c21d1d6fe2def33 to your computer and use it in GitHub Desktop.
Custom URLProtocol monitoring URLRequests over a URLSession in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CustomURLProtocol: URLProtocol { | |
struct Constants { | |
static let RequestHandledKey = "URLProtocolRequestHandled" | |
} | |
var session: URLSession? | |
var sessionTask: URLSessionDataTask? | |
override init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) { | |
super.init(request: request, cachedResponse: cachedResponse, client: client) | |
if session == nil { | |
session = URLSession(configuration: .default, delegate: self, delegateQueue: nil) | |
} | |
} | |
override class func canInit(with request: URLRequest) -> Bool { | |
if CustomURLProtocol.property(forKey: Constants.RequestHandledKey, in: request) != nil { | |
return false | |
} | |
return true | |
} | |
override class func canonicalRequest(for request: URLRequest) -> URLRequest { | |
return request | |
} | |
override func startLoading() { | |
let newRequest = ((request as NSURLRequest).mutableCopy() as? NSMutableURLRequest)! | |
CustomURLProtocol.setProperty(true, forKey: Constants.RequestHandledKey, in: newRequest) | |
sessionTask = session?.dataTask(with: newRequest as URLRequest) | |
sessionTask?.resume() | |
} | |
override func stopLoading() { | |
sessionTask?.cancel() | |
} | |
} | |
extension CustomURLProtocol: URLSessionDataDelegate { | |
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | |
client?.urlProtocol(self, didLoad: data) | |
} | |
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) { | |
let policy = URLCache.StoragePolicy(rawValue: request.cachePolicy.rawValue) ?? .notAllowed | |
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: policy) | |
completionHandler(.allow) | |
} | |
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | |
if let error = error { | |
client?.urlProtocol(self, didFailWithError: error) | |
} else { | |
client?.urlProtocolDidFinishLoading(self) | |
} | |
} | |
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { | |
client?.urlProtocol(self, wasRedirectedTo: request, redirectResponse: response) | |
completionHandler(request) | |
} | |
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) { | |
guard let error = error else { return } | |
client?.urlProtocol(self, didFailWithError: error) | |
} | |
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | |
let protectionSpace = challenge.protectionSpace | |
let sender = challenge.sender | |
if protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { | |
if let serverTrust = protectionSpace.serverTrust { | |
let credential = URLCredential(trust: serverTrust) | |
sender?.use(credential, for: challenge) | |
completionHandler(.useCredential, credential) | |
return | |
} | |
} | |
} | |
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { | |
client?.urlProtocolDidFinishLoading(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment