1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
// // WebViewScriptViewController.swift // UpgradeMySelf-ios // // Created by Shubham Sharma on 20/01/20. // Copyright © 2020 Shubham Sharma. All rights reserved. // import UIKit import WebKit class WebViewScriptViewController: UIViewController, WKNavigationDelegate { @IBOutlet weak var webView: WKWebView! private var webViewContentIsLoaded = false var __url: String = "https://www.google.com/" override func viewDidLoad() { super.viewDidLoad() webView.configuration.userContentController.add(self, name: "paymentResponse") webView.scrollView.bounces = false webView.navigationDelegate = self if !webViewContentIsLoaded { let url = URL(string: __url)! let request = URLRequest(url: url) webView.load(request) webViewContentIsLoaded = true } } private func evaluateJavascript(_ javascript: String, sourceURL: String? = nil, completion: ((_ error: String?) -> Void)? = nil) { var javascript = javascript // Adding a sourceURL comment makes the javascript source visible when debugging the simulator via Safari in Mac OS if let sourceURL = sourceURL { javascript = "//# sourceURL=\(sourceURL).js\n" + javascript } webView.evaluateJavaScript(javascript) { _, error in completion?(error?.localizedDescription) } } // MARK: - WKNavigationDelegate func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { // This must be valid javascript! Critically don't forget to terminate statements with either a newline or semicolon! let javascript = "var message = {\"Status\": \"SUCCESS\", \"transactionId\": \"#PAY123133\" }\n" + "window.webkit.messageHandlers.paymentResponse.postMessage(message)\n" evaluateJavascript(javascript, sourceURL: nil) } func showAlertWithMessage(title: String, message:String ) { let alert = UIAlertController.init(title: title , message:message , preferredStyle:.alert) let action = UIAlertAction.init(title: "OK", style: .cancel) { (action) in } alert.addAction(action) self.present(alert, animated:true, completion: nil) } } extension WebViewScriptViewController: WKScriptMessageHandler { // MARK: - WKScriptMessageHandler func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { print(message.body) guard let body = message.body as? [String: Any] else { print("could not convert message body to dictionary: \(message.body)") showAlertWithMessage(title: "Payment Declined", message: "" ) return } guard let status = body["Status"] as? String else { print("could not convert Status to string: \(body)") showAlertWithMessage(title: "Payment Declined", message: "" ) return } switch status { case "FAILED": showAlertWithMessage(title: "Payment Declined", message: "") print("Transaction Failed") break case "SUCCESS": guard let transactionId = body["transactionId"] as? String else { print("could not transactionId to string: \(body)") return } print("outerHTML is \(transactionId)") showAlertWithMessage(title: "Payment Declined", message: "Transaction Id \(transactionId)" ) break default: print("unknown message type \(status)") return } } } |