问题描述: 最近在做一个纯H5套壳项目,就加载一个网页(使用的是WKWebView); 但是商品加入到购物车之后点击删除没有反应;正常情况下是要谈出一个窗提示是否要删除. 网页用Chrome或Safari打开是正常弹窗的;
** 图片如下: 删除按钮怎么点都没反应 **

尝试解决办法:
- 由于用的是WKWebView,按钮点击应该会拦截到这个点击事件;
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString * requestStr = navigationAction.request.URL.absoluteString;
if ([requestStr containsString:WKCCBPRE]) {
NSArray *resultArray = [requestStr componentsSeparatedByString:@"?"];
[self dopayment:resultArray.lastObject];
decisionHandler(WKNavigationActionPolicyCancel);
}
else{
decisionHandler(WKNavigationActionPolicyAllow);
}
}
但是这个代理方法根本不走,H5只是弹了个窗并未发送网络请求; 发送网络请求在弹窗的按钮上;
-
更换UIWebView; 更换后又出现了问题:
一. 首先是性能问题,加载速度太慢了,严重影响用户体验;
二. 弹窗是可以弹窗了 但是弹窗标题不正确(显示域名);
如下图:
2017-12-12 11.29.08.gif
尝试了 创建UIWebView的分类 实现方法,解决不了;(可能是我的问题,因为我总觉得这个UIWebView太low了,解决不了就没有深入的想办法!);
-
使用SFSafariViewController加载网页;
优点: 一.加载每个链接都有一个真实的进度条(多数app的做法就是用一个假的,先匀速加载到80%,再等网页展示了加速加载剩下的20%);
二. 加载速度很快,用户体验很好.
致命的缺点: 样式固定不能定制化, 顶部和底部两坨根本不想要;
2017-12-12 11.46.21.gif
找到一个去掉顶部和底部的方法:
[self performSelector:@selector(_setShowingLinkPreview:) withObject:@(YES)];
但是还是有bug:

最终解决办法:
换回WKWebView,这个控件用的比较多的也就是
WKNavigationDelegate那几个代理方法,什么开始加载,加载完成,加载失败,拦截Url等;殊不知还有一个WKUIDelegate专门处理UI界面交互的,所以最终的解决方案就是遵循WKUIDelegate的代理并实现代理方法:
///处理alert事件
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
// DLOG(@"msg = %@ frmae = %@",message,frame);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler(NO);
}])];
[alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(YES);
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
最终的效果图:

网友评论