美文网首页
WKWebView对接h5问题合集

WKWebView对接h5问题合集

作者: 青菜白玉堂 | 来源:发表于2021-04-14 13:52 被阅读0次

    1.视频播放

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
        //设置HTML5视频是否允许网页播放 设置为NO则会使用本地播放器
        configuration.allowsInlineMediaPlayback=true;
        self.bgWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) configuration:configuration];
    

    2.链接带空格字符

     NSString *encodedString = @"https://budu-test.manniuhealth.com/oauthbridge/code?bud_c=ciming&clientId=3173c587-aaaf-4012-85e9-cee313f972b1&Authorization=bearer 26e2f127-1373-4dac-a920-887c746e73e1";
    //替换空格,为%20
     NSString *strUrl = [encodedString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
     self.urlString   = strUrl;
    

    3.kvo监听title,url变化

    [self.bgWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        [self.bgWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
        [self.bgWebView addObserver:self forKeyPath:@"URL" options:NSKeyValueObservingOptionNew context:NULL];
    

    监听回调

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
        if ([keyPath isEqualToString:@"estimatedProgress"]) {
           //网页加载进度,在这里可以做网页加载进度条
        }else if ([keyPath isEqualToString:@"title"]){
         //根据网页title设置当前导航title
            self.title = change[@"new"];
            
        }else if ([keyPath isEqualToString:@"URL"]){
            //部分页面跳转加载,不会走wkwebview的代理方法,通过监听url变化可以知道页面的改变
            YuToolNSLog(@"URL变化---%@",change[@"new"]);
            
        }
    }
    
    

    4.打印h5页面栈

     YuToolNSLog(@"backListURL数组---%ld",self.bgWebView.backForwardList.backList.count);
            YuToolNSLog(@"forwardListURL数组---%ld",self.bgWebView.backForwardList.forwardList.count);
            
            for (WKBackForwardListItem * item in self.bgWebView.backForwardList.backList) {
                NSLog(@"h5页面栈---%@",item.URL);
            }
    
    

    5.js与原生交互

    1.拦截url(适用于UIWebView和WKWebView)
    2.JavaScriptCore(只适用于UIWebView,iOS7+)
    3.WKScriptMessageHandler(只适用于WKWebView,iOS8+)
    4.WebViewJavascriptBridge(适用于UIWebView和WKWebView,属于第三方框架)

    1. DSBridge(号称最好用的javascript bridge,国人项目,有详细中文文档和问题反馈渠道。)

    6.h5微信与支付宝支付

    目标:h5支付直接调起微信与支付宝客户端进行支付,支付完成或取消支付后返回至app,并跳转到支付结果页

    单独开一篇文章记录:https://www.jianshu.com/p/f2f5102f9061

    相关文章

      网友评论

          本文标题:WKWebView对接h5问题合集

          本文链接:https://www.haomeiwen.com/subject/hnxwkltx.html