How to handle HelaPay links inside `UIWebView` and `WKWebView`

Handling හෙළPay Links

Setup

Clone this repo.

git clone https://github.com/PayHereDevs/helapay-link-handler.git

To properly handle හෙළPay Links in your iOS WebView project, you will need the following helper class.

helapay-links/HelaPayLinkHandler.swift

Copy it into your Xcode project. Follow one of the following steps, depending on whether you use UIWebView or WKWebView.

For UIWebView

1. Set the web view delegate

class ViewController: UIViewController{
    override func viewWillAppear(_ animated: Bool) {
        // ...

        webView.delegate = self
    }
}
2. Implement the shouldStartLoadWith method

extension ViewController: UIWebViewDelegate{
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {

        // Important check for external suffix
        if let url = request.url, url.absoluteString.hasSuffix("#HKexternal") {
            HelaPayLinkHandler().handleHelaPayLink(url)
            return false
        }
        else{
            return true;
        }

    }
}

For WKWebView

1. Set the web view navigation delegate

class ViewController: UIViewController{
    override func viewWillAppear(_ animated: Bool) {
        // ...

        webView.navigationDelegate = self
    }
}
2. Implement the decidePolicyFor method

extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        // Important check for external suffix
        if let url = navigationAction.request.url, url.absoluteString.hasSuffix("#HKexternal") {
            HelaPayLinkHandler().handleHelaPayLink(url)
            decisionHandler(.cancel)
        }
        else{
            decisionHandler(.allow)
        }
    }
}