Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WKWebView] 适配问题 #16

Open
jschyz opened this issue Sep 4, 2018 · 0 comments
Open

[WKWebView] 适配问题 #16

jschyz opened this issue Sep 4, 2018 · 0 comments

Comments

@jschyz
Copy link
Owner

jschyz commented Sep 4, 2018

为了让 iOS WebView 打开H5快,执行响应提升。现起项目优化 WKWebView ,逐步替换掉 UIWebView。

1. WKWebView 拦截 JS 中的 Alert / Confirm / Prompt

在WKWebview中,js的alert是不会出现任何内容的,必须重写WKUIDelegate委托的runJavaScriptAlertPanelWithMessage message方法,自己处理alert。类似的还有Confirm和prompt也和alert类似,这里我只以alert为例。

// 调用JS的alert()方法
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
 
// 调用JS的confirm()方法
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
 
// 调用JS的prompt()方法
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler;

示例代码:https://github.com/JYSDeveloper/WKWebViewDemo/blob/master/WKWebViewDemo/ViewController.m#L167

验证重点:① Confirm Prompt 的回掉 ② 提示框UI的展示

2. WKWebView 中 window.open 失效 以及 标签带有 target='_blank'

_blank 标签,众所周知,是让浏览器新开一个页面来打开链接,而不是在原网页上打开。

在UIWebView上,只有一个页面,所以会自动在原来的页面上打开新链接。

但是在WKWebView上就不是这样了。

x <a onclick="window.open('http://www.qq.com')">window.open</a>
√ <a onclick="window.open('http://www.qq.com', '_self')">window.open</a>
 
x <a href="https://www.baidu.com" target='_blank'>target _blank</a>
√ <a href="https://www.baidu.com">target _self</a>

示例页面:window.open http://ui.ptlogin2.qq.com/cgi-bin/login?appid=636026402&style=8&s_url=http%3A%2F%2Fxw.qq.com%2Findex.htm <点击注册新页面,或忘记密码>

解决方法:http://www.jianshu.com/p/3a75d7348843

3. WKWebView 白屏问题

白屏划分为三类

其一,浏览器访问H5遇到网络错误、证书错误、连接超时错误等。需拦截此等错误code,并显示友好的UI展示(特殊code需过滤);

其二,进度条引起的白屏现象,如网络重定向时,进度条归为0引起白屏;WKWebView 进度是按照真实的下载数据得出的进度百分数,但是当服务器慢请求时,进度一直为0,引发不好的体验

① 进度条宽度先是100%,然后立马变为 0%

② 频繁刷新,进度条前进后,既然后退了

③ 访问网络超时网址时,进度条停在 0% 处

其三,H5渲染白屏。

4. WKWebView 与 Cookie

Cookie 问题是目前 WKWebView 的一大短板

① WKWebView loadRequest H5的时候,发现 Cookie 没有带上,而切换页面的时候,则可以。这会影响依赖 cookie 做登录校验的 H5

原因在于:WKWebView Cookie 问题在于 WKWebView 发起的请求不会自动带上存储于 NSHTTPCookieStorage 容器中的 Cookie

原理链接:http://www.10tiao.com/html/330/201701/2653578513/1.html

② WKWebView 访问任何域名的网址,都会带上默认的 token 等 cookie 敏感信息。

修复只针对自己的域名做单独处理。

5. 自动抓取分享设置

参考类库:https://github.com/beyondabel/BAWebView/blob/master/BAWebView/BAWebView/BAWebView.m

WKWebVIew介绍:

https://zhihu.com/question/54564198/answer/140030597

附:

http://www.jianshu.com/p/403853b63537

http://www.jianshu.com/p/90a90bd13aac

http://www.jianshu.com/p/7bb5f15f1daa

http://liuyanwei.jumppo.com/2015/10/17/ios-webView.html

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1483682025_enmey

http://www.jianshu.com/p/9513d101e582

github:

https://github.com/DoTalkLily/LYWebViewController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant