本文主要是介绍[Swift]WKWebView拉起支付宝和微信APP支付,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先在项目中导入AlipaySDK和WechatOpenSDK。
再对info.plist中LSApplicationQueriesSchemes添加alipay、weixin。
最后到代理方法中完成拦截操作。
extension WKWebVC : WKScriptMessageHandler {func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {var tempURL: String = navigationAction.request.url?.absoluteString ?? ""let tempScheme = navigationAction.request.url?.scheme ?? ""if tempURL.isBlank == false {let host = navigationAction.request.url?.host ?? ""if host.contains("mclient.alipay.com") {AlipaySDK.defaultService().payInterceptor(withUrl: tempURL, fromScheme: "juzhecpsapp") { ress in// 支付成功或者失败的 回调处理 ["resultCode": "6001", "returnUrl": "", "isProcessUrlPay": "1"]if let resDict = ress as? [String: String] {if let returnUrl = resDict["returnUrl"] {if returnUrl.isBlank == false {if let tempReturnUrl = URL(string: returnUrl) {webView.load(URLRequest(url: tempReturnUrl))}}}}}decisionHandler(.cancel)return}/// alipay:// 支付宝支付/// weixin:// 微信支付/// imeituan:// 美团if tempScheme == "alipay" || tempScheme == "weixin" || tempScheme == "imeituan" {if tempScheme == "alipay" {let ll = tempURL.replacingOccurrences(of: "alipays", with: "juzhecpsapp")tempURL = ll}if let openURL = URL(string: tempURL) {UIApplication.shared.open(openURL, options: [:], completionHandler: nil)decisionHandler(.cancel)return}}}decisionHandler(.allow)}
这篇关于[Swift]WKWebView拉起支付宝和微信APP支付的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!