IOS lets Wkwebview support Nsurlprotocol

Source: Internet
Author: User

After IOS8, Apple introduced the new framework WebKit, which provides a component Wkwebview to replace the UIWebView. A variety of uiwebview problems, faster, take up less memory, a word, Wkwebview is the best choice to load the Web page in the app! We do development is the most related to memory problems, basically all the information on the internet is said Wkwebview memory consumption will be less, but in the end how much less I did the next test, the same load 163 home

Use Wkwebview memory with UIWebView memory

From the memory can be estimated to optimize about 80%, but also from the Web page sliding has indeed improved. So obvious performance improvement but Apple did not completely give up UIWebView also must have his reason, take this article to speak of Nsurlprotocol interception request, Wkwebview compatibility is not uiwebview good, also need developers to do some operation.

WebKit Source Code Analysis

Since Wkwebview is based on the WebKit kernel, we need to import one of these things when we use them.
#import <WebKit/WebKit.h>
Through this we can guess all the wkwebview in the request and some logic must go is the webkit inside of the things, so he for the loading of the page and other operations will not go to the system of the province's URL Loading systems, So it is a matter of course that his request cannot be intercepted by Nsurlprotocol. But whether Wkwebview really and nsurlprotocol a little relationship is still need to study, fortunately WebKit is open source, GitHub is easy to find source code (size is probably 1G multipoint zip, it took me nearly a day to see). Pull down the code directly to search Nsurlprotocol to see if there is any information about

Search results
It does seem to have something to do with Nsurlprotocol, which is also found in the call stack behind the breakpoint.
+ [Nsurlprotocol caninitwithrequest:]
Such words, and then through the Internet to check some data also confirmed my guess, in fact, Wkwebview at the beginning of the call to Nsurlprotocol in the entrance method Caninitwithrequest, but there is no then, That is to say, Wkwebview is related to Nsurlprotocol, just return no at the entrance of Nsurlprotocol, so nsurlprotocol not take over Wkwebview request. We point into the WebKit source of the Customprotocol can see, the overall structure we are similar, but I notice that each Customprotocol entry function has such a judgment:
Entry Function 1
Entry Function 2
(Pink can be temporarily identified as a custom string inside it) through this can be guessed, wkwebview not go nsurlprotocol, but need to satisfy his A rule, he will be in the entry function here return Yes to give you release, The rule is that the scheme of the URL you requested is the same as the customscheme of its internal configuration. But here's a question about why Apple filters out Http/https's popular scheme when using WebKit, and it seems he doesn't recommend developers to use Nsurlprotocol. Next we come to see this customscheme, since the Apple internal rules well certainly can in some way to register a scheme of their own, really can't hook. By flipping through his source discovery eventually all point to a code
[WKBrowsingContextController registerSchemeForCustomProtocol:testScheme];

Method is implemented as

+ (void) Registerschemeforcustomprotocol: (NSString *) Scheme{webprocesspool:: Registerglobalurlschemeashavingcustomprotocolhandlers (scheme);} void Webprocesspool::registerglobalurlschemeashavingcustomprotocolhandlers (const string& urlScheme) {     if (! urlscheme)         return ;    Globalurlschemeswithcustomprotocolhandlers (). Add (urlscheme);      for (auto* processpool:allprocesspools ())        processpool-Registerschemeforcustomprotocol ( Urlscheme);}

  

Through the method name can be seen that this is the way to register Customscheme to WebKit, as long as we register our own customprotocol after the call of the method should be able to. Through his source code also further confirmed my guess (he wrote the same)


WebKit Source code specific implementation

Find a way to do it, but because Registerschemeforcustomprotocol is Wkbrowsingcontextcontroller's class method, So can only use Wkbrowsingcontextcontroller to call, but in WebKit's header file found wkbrowsingcontextcontroller not open out , So we use the nsclassfromstring and Nsselectorfromstring methods to get the class and the corresponding method, the whole code is as follows

   
 //sign up for your protocol[Nsurlprotocol Registerclass:[customprotocolclass]]; //Create Wkwebviewwkwebviewconfiguration * Config =[[Wkwebviewconfiguration alloc] init]; Wkwebview* Wkwebview = [[Wkwebview alloc] Initwithframe:cgrectmake (0,0, [UIScreen mainscreen].bounds.size.width, [UIScreen mainscreen].bounds.size.height] configuration:config];    [Wkwebview Loadrequest:webviewreq];    [Self.view Addsubview:wkwebview]; //Registration SchemeClass CLS = nsclassfromstring (@"Wkbrowsingcontextcontroller"); Sel sel= Nsselectorfromstring (@"Registerschemeforcustomprotocol:"); if([CLS Respondstoselector:sel]) {//requests via HTTP and HTTPS, in the same vein, can be passed through other scheme but to satisfy the ULR Loading System[CLS Performselector:sel Withobject:@"http"]; [CLS Performselector:sel withobject:@"HTTPS"]; }

Worth noting
    • About Private APIs

Because Wkbrowsingcontextcontroller and Registerschemeforcustomprotocol should be private, it is necessary to use the string to do the processing, in an encrypted manner or other can be, the actual measurement can be audited.

Third-party libraries

Github:nsurlprotocol-webkitsupport

IOS lets Wkwebview support Nsurlprotocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.