0

In my application, we are using Segpay for payment there is no proper SDK to integrate, then we make the URL from backed and open that in SafariViewController.

When the user completes the payment successfully we need to bring to a user into the home page, how to we know status is a success from SafariViewController.

I got a response from the backend on SafariViewController like this on webview page: enter image description here

2 Answers2

-1

Instead of safari you can use WKWebview like below.

Create a property of WKWebView 

private var webView: WKWebView!

Initialise webview as follow in viewDidLoad method...

let jScript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let wkUScript = WKUserScript(source: jScript, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(wkUScript)

let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController = wkUController

webView = WKWebView(frame: view.bounds, configuration: webConfiguration)
webView.navigationDelegate = self
view.addSubview(webView)
let url = URL(string: "https://...") //set url to load in webview
let req = URLRequest(url: url)
webView.load(req)

Implement the following delegate method of WKWebView.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    webView.evaluateJavaScript("document.getElementById('response_success').textContent", completionHandler: { (html, error) in

        if let content = html as? String {
            print(content)

            if content.lowercased().contains("ok") { //here you can place the status coming from server for comparison

               //handle success response
            }
        }
    })
    webView.evaluateJavaScript("document.getElementById('response_fail').textContent", completionHandler: { (html, error) in

        if let content = html as? String {
            print(content)

            if content.lowercased().contains("fail") { //here you can place the status coming from server for comparison

               //handle fail response
            }
        }
    })
}

Note: You need to ask your backend team to post data in webview using tag with id response_success, if response is success & response_fail, if response is failed.(You can change it as per your requirements)

Rufat Mirza
  • 1,425
  • 14
  • 20
Mahendra
  • 8,448
  • 3
  • 33
  • 56
-1

It is anyone need to use this code.

For Load URL:

 let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view = webView
        let myURL = URL(string: "YOUR_URL")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)

Delegate Method for handle URL :

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        if let url = webView.url?.absoluteString{
            if url.contains("success.php"){
                print("SUCCESS CASE")
            }else if url.contains("fail.php"){
                print("FAILURE CASE")
            }
        }
    }