1、使用WKWebView的时候,点击链接不让其跳转到系统自带的Safar浏览器的设置方法:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {//跳转别的应用如系统浏览器
// 对于跨域,需要手动跳转
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
// 不允许web内跳转
decisionHandler(WKNavigationActionPolicyCancel);
} else {//应用的web内跳转
decisionHandler (WKNavigationActionPolicyAllow);
}
return ;//不添加会崩溃
}
2、使用WKWebView的时候,点击链接让其跳转到系统自带的Safar浏览器的设置方法:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;
NSString * urlStr = navigationAction.request.URL.absoluteString;
BOOL linkBool = [JCCommonTool isLinkAddressLinkValueTo:urlStr];//自己封装的验证是否是链接的方法
if (linkBool == YES) {
actionPolicy = WKNavigationActionPolicyCancel;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
}
decisionHandler(actionPolicy);
}
3、是否是链接的判断方法
- (void)isLinkUrl:(NSString * )linkStr{
NSString*emailRegex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
return [predicate evaluateWithObject:linkStr];
}