UPI intent from iOS WKWebView

278 views Asked by At

I am trying to open UPI apps from swift WKWebView.

I have tried it using adding openURL into other application if its not http or https.

Here is the strategy I am following. AppDelegate class

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

Here is the Webview navigation delegate function

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)
    }

How can I open the UPI apps the way safari opens them with a dialog saying Open in "GPay".

1

There are 1 answers

0
MuhammedSwalih V H On BEST ANSWER

You can use a custom URL scheme that is registered by UPI apps. UPI apps typically have URL schemes that you can use to initiate payment requests. Here's how you can do it:

  • Find the URL scheme of the UPI app you want to open. Popular UPI apps like Google Pay, PhonePe, or PayTM have their custom URL schemes.

  • Once you have the URL scheme, you can create a link with that scheme and the necessary UPI parameters. For example, to open Google Pay:

    let upiURLString = "upi://pay?pa=recipient@upi&pn=Recipient Name&mc=1234&tid=123456&tr=12345678&am=100&cu=INR"

Here's what each parameter means:

pa: Payee VPA (Virtual Payment Address) pn: Payee name mc: Merchant code (if applicable) tid: Transaction ID tr: Transaction reference ID am: Amount cu: Currency code (e.g., INR for Indian Rupees)

  • Check if the UPI app is installed and open the URL using

    if let upiAppURL = URL(string: upiURLString) { if UIApplication.shared.canOpenURL(upiAppURL) { UIApplication.shared.open(upiAppURL, options: [:], completionHandler: nil) } else { // Handle the case where the UPI app is not installed // You may want to provide a fallback option or notify the user } }

You should place this code in the appropriate event handler in your WKWebView, such as when a button or link is clicked. You can also listen for navigation events in the WKNavigationDelegate to detect when a specific link is clicked and then trigger this action accordingly.