UPI Intent in WebView - iOS
Enable UPI Intent in WebView on your iOS app.
Follow the steps given below to enable UPI intent in WebView on your iOS application:
1.1
1.2
1.3
1.4
Add the code given below to your AppDelegate
. In this example, we override the application(_:open:options:)
method to handle deep link URLs. You can use this method to parse the URL and determine which screen or content to display in your app.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {return true}
Get a reference to your WKWebView
and call the navigationDelegate
property to setup WKWebView
using the following code. Add the loadWebPage
function to load your content.
class CheckoutPaymentViewController: UIViewController {var checkoutUrl: String?// You can assign the checkout URL String to checkoutUrl property or pass it from previous page.@IBOutlet weak var wkWebView: WKWebView!override func viewDidLoad() {super.viewDidLoad()wkWebView.navigationDelegate = selfself.loadWebPage()}func loadWebPage() {guard let urlString = self.checkoutUrl else { return }guard let url = URL(string: urlString) else { return }self.wkWebView.load(URLRequest(url: url))}}
Create a WKNavigationDelegate
to handle navigation events in your WKWebView
.
In this example, we override the WebView(_:decidePolicyFor:decisionHandler:)
method to handle navigation events in the WKWebView. If the URL scheme is not http
or https
, then we check if the URL can be opened using the UIApplication.shared.canOpenURL()
method.
- If yes, then we open the URL using the
UIApplication.shared.open()
method. - If not, then we allow the navigation to continue using the
decisionHandler(.allow)
method.
extension CheckoutPaymentViewController: WKNavigationDelegate {func webView(_ webView: WKWebView,decidePolicyFor navigationAction: WKNavigationAction,decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {// If the request is a non-http(s) schema, then have the UIApplication handle opening the request.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)}else {// Allow the request.decisionHandler(.allow)}}}
To test your app:
- Open your website integrated with standard checkout.
- Pass
webview_intent: true
parameter in the sent to checkout to enable UPI intent support.
Watch Out!
By default, the top PSP apps appear on the customer's mobile irrespective of the installation status of the UPI apps.
Is this integration guide useful?