MTURLProtocol
1. Introduction
MTURLProtocol is a subclass of NSURLProtocl and itself is subclass restricted. It is used to avoid implementing multiple NSURLProtocol subclass in one application because multiple NSURLProtocol subclass will make it difficult to track and debug network traffic. In addition, mutiple NSURLProtocol will affect network efficiency.
If you have implemented multiple NSURLProtocol subclass and find it inconvenient to maintain those code, it's time to consider migrating to use MTURLProtocol to implement the same function.
MTURLProtocol is protocol oriented. The main idea is providing 3 protocols: MTRequestHandler
, MTTaskHandler
, MTResponseHandler
for decorating request, dataTask and response.
2. Structure
2.1 Handle Request
A request will be handled by your (mutiple) MTRequestHandler protocol instance(s) in order of adding time. Before sending decorated request, you have chance to choose one of the MTTaskHandler protocol instance to decorate dataTask used in MTURLProtocl instance.
2.1.1 Handle Local Request
You can implement MTLocalRequestHandler protocol to return response and response data instantly.
2.2 Handle Response
MTURLProtocol instance will choose only one MTResponseHandler protocol instance to handle the response regarding to the original request
: the request before decorated and the final request
: the request before sent.
Previously, you may have multiple NSURLProtocol instances to handle different response. If more than 2 NSURLProtocol instances was handling one request, you may need merge the logic into one MTResponseHandler protocol instance when migrating.
3. Example
See demo in MTURLProtocl-Example
project.
MTTestRequestHandler
+MTTestResponseHandler
: decorating remote request and responseMTTestLocalRequestHandler
: decorating local request and return response instantly- There is no example for
MTTaskHandler
but it is also easy like implementing handlers above.
4. Installation
MTURLProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'MTURLProtocol'
5. Projects and Apps using MTURLProtocol
6. Change Log
- 0.1.2: Minor fix of Podfile source.
- 0.1.1: Add demo.
- 0.1.0: The first version.
7. Author
8. License
MTURLProtocol is available under the MIT license. See the LICENSE file for more info.