https://github.com/victorzhang2014/zrurlprotocol
ZRURLProtocol is encapsulated of NSURLProtocol which can intercept all request in UIWebView
https://github.com/victorzhang2014/zrurlprotocol
interception-httphttps-request ios nsurlprotocol objective-c
Last synced: 10 months ago
JSON representation
ZRURLProtocol is encapsulated of NSURLProtocol which can intercept all request in UIWebView
- Host: GitHub
- URL: https://github.com/victorzhang2014/zrurlprotocol
- Owner: VictorZhang2014
- Created: 2017-02-25T11:38:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-25T12:00:09.000Z (over 9 years ago)
- Last Synced: 2025-07-12T01:33:40.168Z (12 months ago)
- Topics: interception-httphttps-request, ios, nsurlprotocol, objective-c
- Language: Objective-C
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ZRURLProtocol
ZRURLProtocol is encapsulated of NSURLProtocol which can intercept all requests in UIWebView
## Purpose
>> 1. To intercept all requests in UIWebView
>> 2. Want to mock/simulate NSURLRequest while requesting in UIWebView
>> 3. Want to replace some of images in HTML pages requesting by local images
#### Apple Official Demo and Documentation
- [NSURLProtocol Tutorial in Apple Demo](https://developer.apple.com/library/content/samplecode/CustomHTTPProtocol/Introduction/Intro.html)
### For more details , see
- [NSURLProtocol Tutorial](https://www.raywenderlich.com/59982/nsurlprotocol-tutorial)
- [NSURLProtocol Tutorial](http://draveness.me/intercept/)
## Usage
1. In `AppDelegate.h` file
```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[ZRURLProtocol start];
return YES;
}
```
2. Implement two methods in the `ZRURLProtocolDelegate`
```
#import "ViewController.h"
#import "ZRURLProtocol.h"
@interface ViewController ()
@property (nonatomic, strong) UIWebView * webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest * req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.webView];
[self.webView loadRequest:req];
[ZRURLProtocol setDelegate:self];
}
- (void)URLProtocolEveryURLRequestInWebView:(NSURLRequest *)request
{
//Every URL of the request will be invoked here.
NSLog(@"url=%@", request.URL.absoluteString);
}
- (NSURLRequest *)URLProtocolOriginalRequest:(NSURLRequest *)request
{
//1.Returns an new instance of NSURLRequest
//2.Modifies the property of @param-request, like header's value or add new header property
return request;
}
@end
```