WKWebView是iOS8后出來的新框架.
WKWebView的接口簡單介紹:點擊鏈接
WKWebView和UIWebView性能對比分析:點擊鏈接
WKWebView和UIWebView的接口對比介紹:點擊鏈接
使用WKWebView主要遇到三個問題:
-
WKWebView不支持web直接拉起其他app(包括AppStore)
原生代碼通過攔截,自己進(jìn)行跳轉(zhuǎn).
1 . 先獲取可能需要跳轉(zhuǎn)的URL Scheme
self.needOpenPrefixs = [NSMutableArray array];
[self.needOpenPrefixs addObject:@"https://itunes.apple.com"];
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray *queriesSchemes = infoDict[@"LSApplicationQueriesSchemes"];
for (NSString *scheme in queriesSchemes) {
NSString *prefix = [NSString stringWithFormat:@"%@://",scheme];
[self.needOpenPrefixs addObject:prefix];
}
2 . 攔截需要跳轉(zhuǎn),判斷是否安裝,若安裝了進(jìn)行跳轉(zhuǎn)
for (NSString *str in self.needOpenPrefixs) {
if ([requestUrlStr rangeOfString:str options:NSCaseInsensitiveSearch].location == 0) {
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([UIApplication instancesRespondToSelector:@selector(openURL:options:completionHandler:)]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@(NO)} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
}
return;
}
}
}
-
WKWebView和JS交互代碼不同,包括JS端
1 . JS端用如下方式向OC發(fā)送消息
window.webkit.messageHandlers.{NAME}.postMessage()
當(dāng)傳遞參數(shù)為空時,必須傳入null,即:
window.webkit.messageHandlers.{NAME}.postMessage(null)
2 . WKWebView不支持直接返回值
在UIWebView中,支持直接返回值.
例如:
userToken = TingShuJS.getUserToken();
在iOS端注入TingShuJS和getUserToken后,調(diào)用方法
-(NSString *)getUserToken{
NSString *userToken = [LMUserManager shareUserManager].activeUser.token;
return userToken;
}
在JS端可以接收到userToken.
JS端使用對應(yīng)發(fā)送消息方法后,
userToken = window.webkit.messagehandlers.getUserToken(null);
iOS端調(diào)用getUserToken方法后,JS無法接收到userToken.
3 . 其他注意點
WKWebView不再適合下面這種方式來設(shè)置全局UserAgent,只能設(shè)置request的時候自定義UserAgent.
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];
-
target='_blank'的標(biāo)簽無法打開
實現(xiàn)代理如下:
-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
WKFrameInfo *frameInfo = navigationAction.targetFrame;
if (![frameInfo isMainFrame]) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
iOS,Android,JS三端統(tǒng)一方案
基本原理:
注冊handler的時候,把相關(guān)信息存起來.
- (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler {
_base.messageHandlers[handlerName] = [handler copy];
}
攔截url,處理對應(yīng)handler
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (webView != _webView) { return YES; }
NSURL *url = [request URL];
__strong WVJB_WEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewDelegate;
if ([_base isWebViewJavascriptBridgeURL:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
} else if ([_base isQueueMessageURL:url]) {
NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]];
[_base flushMessageQueue:messageQueueString];
} else {
[_base logUnkownMessage:url];
}
return NO;
} else if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
return [strongDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
} else {
return YES;
}
}
iOS webView拉起App問題
iOS 9開始,必須添加URL Scheme,才能正確判斷是否安裝App.
iOS 10 必須添加URL Scheme,webView否則無法判斷應(yīng)用是否安裝,無法拉起App.
- 獲取App的URL Scheme
對ipa進(jìn)行解包,在plist文件中找到URL Type選項,URL Scheme.
例如手機京東:
兩個URL Scheme
openApp.jdMobile
openjd
注意: URL Scheme不區(qū)分大小寫
- 部分App的URL Scheme如下:
注意:有些應(yīng)用區(qū)分iPhone版和iPad版,URL Scheme可能不同
手機京東
openapp.jdmobile
openjd
京東HD
openapp.jdipad
一號店
yhd
貝貝
beibeiapp
當(dāng)當(dāng)
dangdang
dd
當(dāng)當(dāng)HD
dangdanghd
ddhd
國美
gomeeshop
聚美
jumei
jumeimall
jumeiwallet
蘑菇街
mogujie
蘑菇街HD
mgjhd
蘇寧
com.suning.suningebuy
suning
蘇寧
com.suning.suning4ipad
手機淘寶
taobao
淘寶HD
taobaohd
天貓
tmall
唯品會
vipshop
vsspecialswitch
vsspecial