iOS11AdaptationTips icon indicating copy to clipboard operation
iOS11AdaptationTips copied to clipboard

Deal With WKWebView DNS pollution problem in iOS11

Open ChenYilong opened this issue 7 years ago • 6 comments

iOS 11 WKWebView provides APIs which work like NSURLProtocol in UIWebView, it's useful to WKWebView DNS pollution problem.

New API -[WKWebViewConfiguration setURLSchemeHandler:forURLScheme:],provide WKURLSchemeHandler to handle the request with your network tool.

Reference: Apple API Doc

ChenYilong avatar Jun 14 '17 06:06 ChenYilong

unable to handle http and https

[_webView.configuration setURLSchemeHandler:self forURLScheme:@"http"];
[_webView.configuration setURLSchemeHandler:self forURLScheme:@"https"];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ''http' is a URL scheme that WKWebView handles natively'

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ''https' is a URL scheme that WKWebView handles natively'

same with ftp:// file:// data://.

reason

Reference: Source Code

- (void)setURLSchemeHandler:(id <WKURLSchemeHandler>)urlSchemeHandler forURLScheme:(NSString *)urlScheme
{
   auto *urlSchemeHandlers = _urlSchemeHandlers.get([] { return adoptNS([[NSMutableDictionary alloc] init]); });

   if ([WKWebView handlesURLScheme:urlScheme])
       [NSException raise:NSInvalidArgumentException format:@"'%@' is a URL scheme that WKWebView handles natively", urlScheme];

   auto canonicalScheme = WebCore::URLParser::maybeCanonicalizeScheme(urlScheme);
   if (!canonicalScheme)
       [NSException raise:NSInvalidArgumentException format:@"'%@' is not a valid URL scheme", urlScheme];

   if ([urlSchemeHandlers objectForKey:(NSString *)canonicalScheme.value()])
       [NSException raise:NSInvalidArgumentException format:@"URL scheme '%@' already has a registered URL scheme handler", urlScheme];

   [urlSchemeHandlers setObject:urlSchemeHandler forKey:(NSString *)canonicalScheme.value()];
}

why?

the new API function is not for request handling, the function looks like this:

//OC register scheme for JS to invoke
[[_webView configuration].userContentController addScriptMessageHandler:self name:@"closeMe"];

//OC operation after JS method invoked
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
   NSLog(@"JS invoke  %@ method,paramers is %@",message.name,message.body);
}

//JS invoke:
   window.webkit.messageHandlers.closeMe.postMessage(null);

the feature is more like WebViewJavascriptBridge , rather than NSURLProtocol.

ChenYilong avatar Jul 19 '17 03:07 ChenYilong

So can the new API -[WKWebViewConfiguration setURLSchemeHandler:forURLScheme:] deal With WKWebView DNS pollution problem in iOS11?

The answer is CANNOT.

ChenYilong avatar Jul 19 '17 08:07 ChenYilong

iOS11 new API WKHTTPCookieStore can handle the WKWebView Cookie Infomation.

new feature:

 WKHTTPCookieStore *cookieStroe = self.webView.configuration.websiteDataStore.httpCookieStore;
  //get cookies
   [cookieStroe getAllCookies:^(NSArray<NSHTTPCookie *> * _Nonnull cookies) {
       NSLog(@"All cookies %@",cookies);
   }];

   //set cookie
   NSMutableDictionary *dict = [NSMutableDictionary dictionary];
   dict[NSHTTPCookieName] = @"userid";
   dict[NSHTTPCookieValue] = @"123";
   dict[NSHTTPCookieDomain] = @"xxxx.com";
   dict[NSHTTPCookiePath] = @"/";

   NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:dict];
   [cookieStroe setCookie:cookie completionHandler:^{
       NSLog(@"set cookie");
   }];

   //delete cookie
   [cookieStroe deleteCookie:cookie completionHandler:^{
       NSLog(@"delete cookie");
   }];

ChenYilong avatar Jul 20 '17 02:07 ChenYilong

阿西吧 那不是僵硬了 不能处理http和https请求,WKWebView在NSURLProtocol下问题多啊

JeroldLiu777 avatar Aug 31 '17 03:08 JeroldLiu777

@lucifer717 参考这里:https://github.com/ChenYilong/iOSBlog/issues/11

ChenYilong avatar Aug 31 '17 03:08 ChenYilong

@ChenYilong OK thx

JeroldLiu777 avatar Sep 28 '17 01:09 JeroldLiu777