Created
January 9, 2017 12:36
-
-
Save ricardopereira/77656bc9bbca8a7e311ca7ffa9e8ecbe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// by calling [NSURLProtocol registerClass:[MyURLProtocol class]]; in -application:didFinishLoadingWithOptions:, your protocol will have priority over any of the built-in protocols. | |
URLProtocol.registerClass(ActivityURLProtocol.self) | |
//URLProtocol.unregisterClass(ActivityURLProtocol.self) | |
print(URLSession.shared.configuration.protocolClasses) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
let photoURL = URL(string: "https://jxccsa.bn1.livefilestore.com/y2m5XyfjXFW78kXvn3-McCXl5926JSzKH-n7hysppnCZxn_hnMPjwWA2dT11K_pInOZYJArvCzlh0_Aozw2ZK_4sBMspJx23DN1kNX8IfRW4_cBuasM9pEUvOTup6p9KCCm/AE1.jpg?psid=1")! | |
URLSession.shared.dataTask(with: photoURL) { data, response, error in | |
print(response) | |
}.resume() | |
} | |
} | |
class ActivityURLProtocol: URLProtocol { | |
// References: | |
// - http://nshipster.com/nsurlprotocol/ | |
// - https://www.raywenderlich.com/59982/nsurlprotocol-tutorial | |
// - https://developer.apple.com/reference/foundation/nsurlprotocol | |
// NSURLProtocolClient protocol: | |
// - Each instance of a NSURLProtocol subclass has a client property, which is the object that is communicating with the URL Loading system. | |
var connection: NSURLConnection? | |
override class func canInit(with request: URLRequest) -> Bool { | |
// If the method returns YES, then the loading system will rely on this NSURLProtocol subclass to handle the request, and ignore all other handlers. If none of the custom registered handlers can handle the request, then the URL Loading System will handle it by itself, using the system’s default behavior. | |
// True means: it’s entirely your class’s responsibility to handle everything about that request. | |
return false | |
} | |
override public func startLoading() { | |
print("startLoading") | |
UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
connection = NSURLConnection(request: request, delegate: self) //Depecrated! | |
} | |
override public func stopLoading() { | |
print("stopLoading") | |
UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment