項目中直接用的WKWebView,最近有需求點擊cell調(diào)到webview筹吐,然后在網(wǎng)頁中點擊下載按鈕,跳轉(zhuǎn)至appstore纪岁,但是沒有任何反應(yīng)伺帘。
起初以為是web代碼的原因昭躺,但是跟web確認(rèn)后發(fā)現(xiàn)沒有問題
在WKWebView代理方法
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
錯誤信息是:
Error Domain=WebKitErrorDomain Code=102 "幀框加載已中斷" UserInfo={_WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x1c0c3f020>, NSErrorFailingURLStringKey=itmss://itunes.apple.com/cn/app/*****, NSErrorFailingURLKey=itmss://itunes.apple.com/cn/app/****, NSLocalizedDescription=幀框加載已中斷}
網(wǎng)上查閱資料忌锯,發(fā)現(xiàn)是WKWebView自身的問題伪嫁。。偶垮。
解決方法也挺簡單
在WKWebView的另一個方法:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
/* 簡單判斷host张咳,真實App代碼中帝洪,需要更精確判斷itunes鏈接 */
if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] && [[UIApplication sharedApplication] openURL:navigationAction.request.URL]){
policy =WKNavigationActionPolicyCancel;
}
decisionHandler(policy);
}
OK!