Git Product home page Git Product logo

nsurlprotocol-webkitsupport's Introduction

NSURLProtocol+WebKitSupport

Note:If you target iOS 11, you can now use WKURLSchemeHandler to avoid this trick! 😆

让 WKWebView 支持 NSURLProtocol

This example project shows a way to use NSURLProtocol with WKWebView, which was not possible before.

Screenshot

Usage

Drag NSURLProtocol+WebKitSupport.h and NSURLProtocol+WebKitSupport.m into your project, then register the scheme for NSURLProtocol to handle:

[NSURLProtocol wk_registerScheme:@"https"];

// You can now use your own NSURLProtocol subclasses as before.
[NSURLProtocol registerClass:[MyAwesomeURLProtocol class]];

To remove the scheme from registery:

[NSURLProtocol wk_unregisterScheme:@"https"];

Note

This category uses undocumented APIs in WebKit. Use at your own risk.

nsurlprotocol-webkitsupport's People

Contributors

yeatse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nsurlprotocol-webkitsupport's Issues

ios10.3 unsupported?

logging: 2017-08-09 15:56:09.732903+0800 WebviewDemo[1022:227643] Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service

WebKit::ChildProcessProxy::sendMessage crash 崩溃

crash堆栈日志如下:

0 WebKit 0x00000001ea9a9230 WebKit::ChildProcessProxy::sendMessage(std::__1::unique_ptr<IPC::Encoder, std::__1::default_deleteIPC::Encoder >, WTF::OptionSetIPC::SendOption) + 24
1 WebKit 0x00000001eaaee84c bool WebKit::ChildProcessProxy::sendMessages::LegacyCustomProtocolManager::DidFailWithError(Messages::LegacyCustomProtocolManager::DidFailWithError&&, unsigned long long, WTF::OptionSetIPC::SendOption) + 128
2 WebKit 0x00000001eaaee7c0 WebKit::LegacyCustomProtocolManagerProxy::didFailWithError(unsigned long long, WebCore::ResourceError const&) + 40
3 WebKit 0x00000001ea84c290 -[WKCustomProtocolLoader connection:didFailWithError:] + 72
4 CFNetwork 0x00000001db7ded04 ___65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 68
5 CFNetwork 0x00000001db566760 -[NSURLConnectionInternalConnection invokeForDelegate:] + 164
6 CFNetwork 0x00000001db55db44 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 212
7 CFNetwork 0x00000001db56281c -[NSURLConnectionInternal _withConnectionAndDelegate:] + 60
8 CFNetwork 0x00000001db79d590 _NSURLConnectionDidFail(_CFURLConnection*, __CFError*, void const*) + 96
9 CFNetwork 0x00000001db6e7a3c ____ZN27URLConnectionClient_Classic17_delegate_didFailEP9__CFErrorU13block_pointerFvvE_block_invoke + 124
10 CFNetwork 0x00000001db6e528c ____ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 128
11 libdispatch.dylib 0x00000001da9c1884 __dispatch_client_callout + 20
12 libdispatch.dylib 0x00000001da9c4e5c __dispatch_block_invoke_direct + 252
13 CFNetwork 0x00000001db7bc5ec RunloopBlockContext::_invoke_block(void const*, void*) + 44
14 CoreFoundation 0x00000001daee1750 _CFArrayApplyFunction + 84
15 CFNetwork 0x00000001db7bc4b0 RunloopBlockContext::perform() + 132
16 CFNetwork 0x00000001db7bd96c MultiplexerSource::perform() + 376
17 CFNetwork 0x00000001db7bd618 MultiplexerSource::_perform(void*) + 64
18 CoreFoundation 0x00000001daf42444 _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28
19 CoreFoundation 0x00000001daf423c0 ___CFRunLoopDoSource0 + 92
20 CoreFoundation 0x00000001daf41ccc ___CFRunLoopDoSources0 + 260
21 CoreFoundation 0x00000001daf3c950 ___CFRunLoopRun + 988
22 CoreFoundation 0x00000001daf3c254 _CFRunLoopRunSpecific + 452
23 GraphicsServices 0x00000001dd17bd8c _GSEventRunModal + 108
24 UIKitCore 0x00000002082844c0 _UIApplicationMain + 216
25 WKMainProject 0x0000000104913284 main (main.m:14)
26 libdyld.dylib 0x00000001da9f8fd8 _start + 4

我们项目中也使用wk_registerScheme注册 scheme,且注册有多个,有谁遇到过吗?

请问这个工具可以解决wkwebview的缓存问题吗?

最近初学swift做一个Mac应用,用到了wkwebview,但是遇到一个问题,当打开几个页面后再返回到之前的页面,会重新发送网络请求重载页面,网上查了一下,貌似wkwebview不支持缓存,wkwebview的macOS SDK更加不多,所以想知道这个工具能不能解决这个问题。谢谢!

实际使用时遇到的问题想讨论下.

我想利用WKWebview支持NSURLProtocol后实现类似页面广告屏蔽的功能.
但是实际运用时发现,一旦调用wk_registerScheme:方法注册监听scheme后,一些POST的表单提交就会出现问题.即使在canInitWithRequest:方法中设置对于POST请求的request不处理也不能解决问题.

用代理跟踪请求时发现,未使用NSURLProtocol进行拦截的POST请求中,HTTPBody里包含了表单post的数据.但是使用了NSURLProtocol后,这些请求发送出去的时候没有携带参数.导致请求结果与预想的出现偏差.

如果您有解决方案或者思路,可否一起讨论一下.谢谢~

http post body丢失问题

           URLProtocol.wk_register(scheme: "https")
           URLProtocol.wk_register(scheme: "http")
           URLProtocol.registerClass(WebPURLProtocol.self)

后post body丢失你是怎么解决的?

ViewController释放后WKWebView还在加载导致奔溃

您好,我使用你的方式注册NSURLProtocol。使用的时候发现有个问题。如果ViewController关闭页面,WKWebView还在加载中,并且在ViewController dealloc中已经把WKWebView UIDelegate和navigationDelegate设为nil并stopLoading 但还是会因为已经释放,地址找不到报以下错误。
WKCustomProtocolLoader retain]: message sent to deallocated instance
详细的堆栈信息

 CoreFoundation`CFRelease + 20, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x89f4e833)
    frame #0: 0x2ace6dc8 CoreFoundation`CFRelease + 20
    frame #1: 0x2ebcf570 WebKit`WTF::HashTable<unsigned long long, WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> >, WTF::KeyValuePairKeyExtractor<WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> > >, WTF::IntHash<unsigned long long>, WTF::HashMap<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader>, WTF::IntHash<unsigned long long>, WTF::HashTraits<unsigned long long>, WTF::HashTraits<WTF::RetainPtr<WKCustomProtocolLoader> > >::KeyValuePairTraits, WTF::HashTraits<unsigned long long> >::deallocateTable(WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> >*, int) + 36
    frame #2: 0x2ebcf504 WebKit`WTF::HashTable<unsigned long long, WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> >, WTF::KeyValuePairKeyExtractor<WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> > >, WTF::IntHash<unsigned long long>, WTF::HashMap<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader>, WTF::IntHash<unsigned long long>, WTF::HashTraits<unsigned long long>, WTF::HashTraits<WTF::RetainPtr<WKCustomProtocolLoader> > >::KeyValuePairTraits, WTF::HashTraits<unsigned long long> >::rehash(int, WTF::KeyValuePair<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader> >*) + 128
    frame #3: 0x2ebcf2f2 WebKit`WTF::HashMap<unsigned long long, WTF::RetainPtr<WKCustomProtocolLoader>, WTF::IntHash<unsigned long long>, WTF::HashTraits<unsigned long long>, WTF::HashTraits<WTF::RetainPtr<WKCustomProtocolLoader> > >::remove(unsigned long long const&) + 42
    frame #4: 0x2ebcea54 WebKit`-[WKCustomProtocolLoader connectionDidFinishLoading:] + 96
    frame #5: 0x2a939f80 CFNetwork`__65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 56
    frame #6: 0x2a939f3a CFNetwork`-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 182
    frame #7: 0x2a93a02c CFNetwork`-[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 36
    frame #8: 0x2a84f0ce CFNetwork`___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 78
    frame #9: 0x2a8eb888 CFNetwork`___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 60
    frame #10: 0x2a83ff4c CFNetwork`RunloopBlockContext::_invoke_block(void const*, void*) + 60
    frame #11: 0x2acf1e2c CoreFoundation`CFArrayApplyFunction + 36
    frame #12: 0x2a83fe06 CFNetwork`RunloopBlockContext::perform() + 182
    frame #13: 0x2a83fccc CFNetwork`MultiplexerSource::perform() + 216
    frame #14: 0x2a83fb60 CFNetwork`MultiplexerSource::_perform(void*) + 48
    frame #15: 0x2ada6d56 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
    frame #16: 0x2ada61f8 CoreFoundation`__CFRunLoopDoSources0 + 364
    frame #17: 0x2ada47cc CoreFoundation`__CFRunLoopRun + 772
    frame #18: 0x2acf23c0 CoreFoundation`CFRunLoopRunSpecific + 476
    frame #19: 0x2acf21d2 CoreFoundation`CFRunLoopRunInMode + 106
    frame #20: 0x320f00a8 GraphicsServices`GSEventRunModal + 136
    frame #21: 0x2e301fa0 UIKit`UIApplicationMain + 1440
  * frame #22: 0x0027e04c **`main(argc=1, argv=0x008bda4c) + 106 at main.m:14
    frame #23: 0x38b16aae libdyld.dylib`start + 2

错误发生在这里[WKCustomProtocolLoader connectionDidFinishLoading:]
如果我不使用NSURLProtocol 则不会有问题。我想应该是WKCustomProtocolLoader还不完善所以才没有公布NSURLProtocol

使用这个分类造成崩溃

偶尔会出现崩溃的,
Crashlytics崩溃日志如下:
0 | WebKit | WebKit::LegacyCustomProtocolManagerProxy::stopLoading(unsigned long long) + 20
1 | CFNetwork | __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 60
2 | CFNetwork | -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 200
3 | CFNetwork | -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 56
4 | CFNetwork | invocation function for block in URLConnectionClient_Classic::_delegate_didFinishLoading(void () block_pointer) + 100
5 | CFNetwork | invocation function for block in URLConnectionClient_Classic::_withDelegateAsync(char const*, void (_CFURLConnection*, CFURLConnectionClientCurrent_VMax const*) block_pointer) + 108
6 | libdispatch.dylib |  

2018-05-19 4 55 54

WebKit::LegacyCustomProtocolManagerProxy::stopLoading(unsigned long long) + 20

Crashed: com.apple.main-thread
0 WebKit 0x192d677a8 WebKit::LegacyCustomProtocolManagerProxy::stopLoading(unsigned long long) + 20
1 CFNetwork 0x183b60538 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 60
2 CFNetwork 0x183b604dc -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 200
3 CFNetwork 0x183b60650 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 56
4 CFNetwork 0x183a57ed0 invocation function for block in URLConnectionClient_Classic::_delegate_didFinishLoading(void () block_pointer) + 100
5 CFNetwork 0x183a55d88 invocation function for block in URLConnectionClient_Classic::_withDelegateAsync(char const*, void (_CFURLConnection*, CFURLConnectionClientCurrent_VMax const*) block_pointer) + 108
6 libdispatch.dylib 0x182c54a60 _dispatch_client_callout + 16
7 libdispatch.dylib 0x182c5c170 _dispatch_block_invoke_direct$VARIANT$mp + 224
8 CFNetwork 0x183b37e40 RunloopBlockContext::_invoke_block(void const*, void*) + 36
9 CoreFoundation 0x183228680 CFArrayApplyFunction + 80
10 CFNetwork 0x183b37d00 RunloopBlockContext::perform() + 128
11 CFNetwork 0x183b38ec4 MultiplexerSource::perform() + 312
12 CFNetwork 0x183b38c24 MultiplexerSource::_perform(void*) + 60
13 CoreFoundation 0x18330b404 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 24
14 CoreFoundation 0x18330ac2c __CFRunLoopDoSources0 + 276
15 CoreFoundation 0x18330879c __CFRunLoopRun + 1204
16 CoreFoundation 0x183228da8 CFRunLoopRunSpecific + 552
17 GraphicsServices 0x18520e020 GSEventRunModal + 100
18 UIKit 0x18d248758 UIApplicationMain + 236
19 BluedInternational 0x100c0aa4c main (main.m:13)
20 libdyld.dylib 0x182cb9fc0 start + 4

About WKURLSchemeHandler

Note:If you target iOS 11, you can now use WKURLSchemeHandler to avoid this trick! 😆

But, comment said :
"A class conforming to the WKURLSchemeHandler protocol provides methods for
loading resources with URL schemes that WebKit doesn't know how to handle itself."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.