0、WKWebview加载在线网页,解决中文乱码
NSString*str = [URL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL * urlstr= [NSURL URLWithString:str];
[_wkWebView loadRequest:[NSMutableURLRequest requestWithURL:urlstr cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15]];
上面如果包含#的url会出现问题,使用下面的方法
- (NSString *)strUTF8Encoding:(NSString *)str {
return [[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] stringByReplacingOccurrencesOfString:@"%23" withString:@"#"];
}
1、WKWebview添加手势
UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
swipe.delegate = self;
[wkWebView addGestureRecognizer:swipe];
// 允许多个手势并发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
2、WKWebview禁用弹簧滑动
wkWebView.scrollView.bounces = NO;
3、WKWebview被js调用
window.webkit.messageHandlers.<对象名>.postMessage(body:<数据>)//body中可以直接放js对象,也可以省略body
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"JS 调用了 %@ 方法,传回参数 %@",message.name,message.body);
NSMutableDictionary *dic=[message.body objectForKey:@"body"];
}
4、WKWebview调用js
NSData *data=[NSJSONSerialization dataWithJSONObject:self->object options:NSJSONWritingPrettyPrinted error:nil];
NSString *dataJson=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString *dataFunc=[NSString stringWithFormat:@"generalChart(%@,%lu)",dataJson,(unsigned long)self->chartType];
[self evaluateJavaScript:dataFunc completionHandler:^(id _Nullable param, NSError * _Nullable error) {
}];
5、wkwebview手势返回抖屏问题:
webview侧滑返回后会接着触发html页面添加的transition动画导致
[wkWebView setAllowsBackForwardNavigationGestures:true];//设置手势返回
6、wkwebview无法跳转App Store
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler
{
WKNavigationActionPolicy policy =WKNavigationActionPolicyAllow;
if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] &&[[UIApplication sharedApplication] openURL:navigationAction.request.URL])
{
policy =WKNavigationActionPolicyCancel;
}
decisionHandler(policy);
}
wkwebview无法跳转企业安装
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler
{
NSString *strRequest = [navigationAction.request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([strRequest containsString:@"itms-services"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strRequest]];
}
decisionHandler(policy);
}
7、wkwebview监听事件
[webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"canGoForward" options: