UIWebView更换WKWebView

一、加载方式

1.-(void)loadRequest:(NSURLRequest *)request;

可以请求本地文件也可以请求网络资源

NSURL*fileURL = [[NSBundle mainBundle] URLForResource:@"test.html" withExtension:nil];

//    fileURL= [NSURL URLWithString:@"https://www.baidu.com"];

    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];

    [self.webView loadRequest:request];

2.-(void)loadHTMLString:(NSString *)htmlbaseURL:(NSURL *)url;

只能用于加载本地HTML文件

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"test.html" withExtension:nil];

    NSString *appHtml = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];

    [self.webView loadHTMLString:appHtmlbaseURL:fileURL];


二、代理设置

UIWebView代理设置

 webView.delegate = self;

WKWebView代理设置

self.webView.navigationDelegate = self;


UIWebView与WKWebView相对应的代理

UIWebViewDeletage 一共四个方法

//刚开始加载网页,用于跳转的网页,也可不做任何跳转,也会用于对JS的交互

UIWebView —webView:shouldStartLoadWithRequest:navigationType:

WKWebView —webView:decidePolicyForNavigationAction:decisionHandler:

//已经开始加载网页

UIWebView —webViewDidStartLoad:

WKWebView —webView:didStartProvisionalNavigation:

//加载完成

UIWebView —webViewDidFinishLoad:

WKWebView —webView:didFinishNavigation:

//加载失败

UIWebView —webView:didFailLoadWithError:

WKWebView — webView:didFailLoadWithError:


三、替换中遇到的坑

1.问题:ionic 1不能兼容WKWebView

  解决方法: 只能升级ionic了~~~

2.问题:Xib里不能直接拖WKWebView替换UIWebView,要重写loadView方法

  解决方法:只是拖了一个UIView,再在代码中初始化WKWebView

3.问题:WKWebView不支持scalesPageToFit属性

  解决方法:网上找的方法,在初始化时设置Configuration

[[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];


- (WKWebViewConfiguration *)configurationForScalesPageToFit {

    NSString *jScript = @"var meta = document.createElement('meta');

meta.setAttribute('name', 'viewport'); meta.setAttribute('content',

'width=device-width');

document.getElementsByTagName('head')[0].appendChild(meta);";

    WKUserScript*wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

    WKUserContentController *wkUController = [[WKUserContentController alloc] init];

    [wkUController addUserScript:wkUScript];

    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

    wkWebConfig.userContentController= wkUController;

    returnwkWebConfig;

}


4. 问题:cookies问题,有些请求需要cookies,原来的UIWebView是系统自动设置的,WKWebView需要手动设置.

解决方法: WKWebView手动设置cookies.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

       //添加cookies;

       NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

       request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

        [self.webView loadRequest:request];


5.问题:stringByEvaluatingJavaScriptFromString方法在WKWebView不可用

  解决方法: 用evaluateJavaScript:completionHandler代替,与JS交互

[self.pieWebView evaluateJavaScript:str completionHandler:nil];


6.问题:与JS交互的桥WebViewJavascriptBridge不支持WKWebView

  解决方法:使用WKWebViewJavascriptBridge


7.问题:webView中的图片显示不出来,原因是跨域的图片,html没有带cookie

解决方法:拼接cookie,并且传入加载的html中通过

- (WKWebViewConfiguration *)createWebViewConfiguration {

    WKUserContentController *userContrntController = [[WKUserContentController alloc] init];

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

    configuration.userContentController= userContrntController;

    WKProcessPool*processpool = [[WKProcessPool alloc] init];

    configuration.processPool=processpool;

    //给html cookie值

    WKUserScript*cookieScript = [[WKUserScript alloc] initWithSource:[self cookieJavaScriptString] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];

    [userContrntController addUserScript:cookieScript];

    returnconfiguration;

}


//配置config,获取cookieString

- (NSString *)cookieJavaScriptString {

    NSMutableString*cookieString = [[NSMutableString alloc] init];

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

    for (NSHTTPCookie*cookie incookies) {

        NSString*excuteJSString = [NSString stringWithFormat:@"document.cookie='%@=%@';",cookie.name,cookie.value];

        [cookieString appendString:excuteJSString];

}

    returncookieString;

}

你可能感兴趣的:(UIWebView更换WKWebView)