[iOS开发]WKWebView加载的URL跳转app(微信支付和支付宝支付)

今天项目中集成网页,跳转app支付的问题

解决思路:在网页每次拉起支付的时候,会调用“weixin://XXXXX”和“alipay://xxxxxxxxxx”
原理和打电话,邮件类似
在iOS10.* 有一个方法:

 // Options are specified in the section below for openURL options. An empty options dictionary will result in the same
    // behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather
    // than returning a result.
    // The completion handler is called on the main queue.
    @available(iOS 10.0, *)
    open func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], completionHandler completion: ((Bool) -> Void)? = nil)

在webview代理方法里面实现这样的方法就能实现了
swift5.0

    // 处理拨打电话以及Url跳转等等
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
        printLog("decidePolicyForurl == \(navigationAction.request.url)");
        if navigationAction.request.url?.scheme == "tel" {
//            DispatchQueue.main.async {
                UIApplication.shared.openURL(navigationAction.request.url!);
                decisionHandler(WKNavigationActionPolicy.cancel)
//            }
        }
        else if navigationAction.request.url?.scheme == "sms"{
            //短信的处理
            UIApplication.shared.openURL(navigationAction.request.url!);
            decisionHandler(WKNavigationActionPolicy.cancel)
        }
        else if navigationAction.request.url?.scheme == "mailto"{
            //邮件的处理
            UIApplication.shared.openURL(navigationAction.request.url!);
            decisionHandler(WKNavigationActionPolicy.cancel)
        }
        else if navigationAction.request.url?.scheme == "alipay" || navigationAction.request.url?.scheme == "weixin" {
            if #available(iOS 10.0, *){
                UIApplication.shared.open(navigationAction.request.url!, options: [UIApplication.OpenExternalURLOptionsKey.universalLinksOnly : false]) { (isfinish) in
                    
                }
            }else{
                UIApplication.shared.openURL(navigationAction.request.url!);
            }
            decisionHandler(WKNavigationActionPolicy.allow)
           
        }
        else{
            decisionHandler(WKNavigationActionPolicy.allow)
        }
    }

你可能感兴趣的:([iOS开发]WKWebView加载的URL跳转app(微信支付和支付宝支付))