本文主要是介绍WKWebView 使用的一些踩坑记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// WKwebVeiew 不能调用系统的长按复制
处理方法
// 选择CSS
NSString *css = @"body{-webkit-user-select:auto;-webkit-user-drag:auto;}";
// CSS选中样式
NSMutableString *javascript = [NSMutableString string];
[javascript appendString:@"var style = document.createElement('style');"];
[javascript appendString:@"style.type = 'text/css';"];
[javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
[javascript appendString:@"style.appendChild(cssContent);"];
[javascript appendString:@"document.body.appendChild(style);"];
// javascript注入
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:noneSelectScript];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
_webView = [[MCWKWebView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) configuration:configuration];
_webView.scrollView.scrollEnabled = _scrollEnabled;
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
[self.view addSubview:_webView];
// WKwebVeiew 不能调用APPStore
// 处理方式
NSString *requestString = navigationAction.request.URL.absoluteString;
NSLog(@"------requestString == %@",requestString);
if ([requestString containsString:@"itunes.apple.com"]) {
NSURL *url = navigationAction.request.URL;
UIApplication *app = [UIApplication sharedApplication];
NSLog(@"url.absoluteString == %@", url.absoluteString);
if ([app canOpenURL:url]) {
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
decisionHandler(WKNavigationActionPolicyAllow);
// WKwebView 不能调用打电话
//处理方式
NSURL *URL = navigationAction.request.URL;NSString *scheme = [URL scheme];UIApplication *app = [UIApplication sharedApplication];// 打电话if ([scheme isEqualToString:@"tel"]) {if ([app canOpenURL:URL]) {[app openURL:URL];// 一定要加上这句,否则会打开新页面decisionHandler(WKNavigationActionPolicyCancel);return;} }
/**
* 可以指定配置对象、导航动作对象、window特性
*
* @param webView 实现该代理的webview
* navigation 当前navigation
*/
-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
/**
* 页面开始加载时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
/**
* 当内容开始返回时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
/**
* 页面加载完成之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/**
* 加载失败时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
* @param error 错误
*/
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
/**
* 接收到服务器跳转请求之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
/**
* 在收到响应后,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationResponse 当前navigation
* @param decisionHandler 是否跳转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
/**
* 在发送请求之前,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationAction 当前navigation
* @param decisionHandler 是否调转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
#pragma mark - WKUIDelegate
/**
* 处理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))completionHandler
这篇关于WKWebView 使用的一些踩坑记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!