https://github.com/wilddylan/wkwebviewwithurlprotocol
WKWebView With URLProtocol
https://github.com/wilddylan/wkwebviewwithurlprotocol
wkwebview
Last synced: 3 months ago
JSON representation
WKWebView With URLProtocol
- Host: GitHub
- URL: https://github.com/wilddylan/wkwebviewwithurlprotocol
- Owner: wilddylan
- License: mit
- Created: 2016-11-14T05:00:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-22T03:10:48.000Z (over 7 years ago)
- Last Synced: 2025-05-13T01:16:19.623Z (about 1 year ago)
- Topics: wkwebview
- Language: Objective-C
- Size: 35.2 KB
- Stars: 183
- Watchers: 10
- Forks: 29
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WKWebViewWithURLProtocol
:Warning:
> Note:If you target iOS 11, you can now use WKURLSchemeHandler to avoid this trick! 😆
###### introductions
[](https://opensource.org/licenses/mit-license.php)
After `iOS8.0`, `WKWebView` want to instead of `UIWebView`, but with some defects such as: URLProtocol can't process the request from WKWebView.
`WKWebViewWithURLProtocol` want to fix this use runtime. Now-coming!
But Notice, WKWebView ignored HTTP Body in request, it's a bug, resolve with `Javascript bridge` or `NSURLSession`.
```objc
[NSURLProtocol wk_registerScheme:@"http"];
[NSURLProtocol wk_registerScheme:@"https"];
[NSURLProtocol registerClass:[URLProtocol class]];
```
`URLProtocol` is the subclass of `NSURLProtocol`, before this, use `wk_registerScheme` to let `URLProtocol` known what scheme can be hooked from WKWebView. Now this demo want to hook `http` and `https` scheme.
```objc
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.devdylan.cn"]]];
```
Now, open your custom URLProtocol, set some breakpoint or logs, oh my god! it's worked!
> Notes: It's only useful when systemVersion > `iOS8.4`
Some questions:
Q: Used private API when in review:
A: [Watch this issue](https://github.com/brave/browser-ios/issues/96#issuecomment-260365341)
###### Installation
```ruby
pod "WKWebViewWithURLProtocol"
```
###### Author
Dylan, dylan@china.com
###### Thanks
@yeatse, Original repo without cocoapods
###### License
MIT License.
[](https://app.fossa.io/projects/git%2Bgithub.com%2FWildDylan%2FWKWebViewWithURLProtocol?ref=badge_large)