UPI intent from iOS WKWebView

pw136qt2  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(104)

我正在尝试从Swift WKWebView打开UPI应用程序。
我已经尝试过使用添加openURL到其他应用程序,如果它不是http或https。
这是我遵循的策略。AppDelegate类

import Foundation
import UIKit
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return true
    }
}

下面是Webview导航委托函数

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        currentWebView = webView
        
        guard let url = navigationAction.request.url else {
            decisionHandler(.allow)
            return
        }
        
        print("NavAction URL: \(url)")
        
        if navigationAction.targetFrame == nil {
            // Open in the same tab if the target frame is nil (new tab)
            print("NavAction 1")
            currentWebView?.load(navigationAction.request)
            decisionHandler(.cancel)
            return
        }
        
        else if let url = navigationAction.request.url,
           !url.absoluteString.hasPrefix("http://"),
           !url.absoluteString.hasPrefix("https://"),
           UIApplication.shared.canOpenURL(url) {
            
            // Have UIApplication handle the url (sms:, tel:, mailto:, ...)
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            
            // Cancel the request (handled by UIApplication).
            decisionHandler(.cancel)
        }
        
        decisionHandler(.allow)
    }

我如何才能打开UPI应用程序的方式Safari打开他们的对话框说打开在“GPay”。

8hhllhi2

8hhllhi21#

您可以使用UPI应用程序注册的自定义URL方案。UPI应用程序通常具有URL方案,您可以使用这些方案发起付款请求。你可以这样做:

  • 找到您要打开的UPI应用程序的URL方案。流行的UPI应用程序,如Google Pay,PhonePe或PayTM都有自己的自定义URL方案。
  • 一旦你有了URL方案,你就可以用这个方案和必要的UPI参数创建一个链接。例如,打开Google Pay:

let upiURLString =“upi://pay?pa=收件人@upi&pn=收件人姓名&mc=1234&tid=123456&tr=12345678&am=100&cu=INR”
以下是每个参数的含义:
pa:收款人VPA(虚拟付款地址)pn:收款人姓名mc:商户代码(如适用)tid:交易ID tr:交易参考ID am:金额cu:货币代码(例如,印度卢比的INR)

  • 检查是否安装了UPI应用程序,并使用

如果let upiAppURL = URL(字符串:upiURLString){ if UIApplication.shared.canOpenURL(upiAppURL){UIApplication.shared.open(upiAppURL,options:[:],完成时间:nil)} else { //处理未安装UPI应用程序的情况//您可能希望提供回退选项或通知用户} }

  • 您应该将此代码放置在WKWebView的相应事件处理程序中,例如单击按钮或链接时。您还可以侦听WKNavigationDelegate中的导航事件,以检测特定链接何时被单击,然后相应地触发此操作。*

相关问题