Last active
August 6, 2021 04:19
-
-
Save Xaxxis/4a9d90ecf7adc0c3013e2f323a1e9b74 to your computer and use it in GitHub Desktop.
Snap-WkWebView-Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// midtrans-snap-WebView-sample | |
// | |
import UIKit | |
import WebKit | |
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate { | |
var webView: WKWebView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
webView = WKWebView() | |
webView.uiDelegate = self | |
webView.navigationDelegate = self | |
view = webView | |
// Put Snap URL here | |
let snapUrl = "https://app.midtrans.com/snap/v2/vtweb/58db5068-13f0-468e-9c5f-c8a84ad540c3" | |
let url = URL(string: snapUrl)! | |
webView.load(URLRequest(url: url)) | |
} | |
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 non-https(s) scheme | |
UIApplication.shared.open(url, options: [:], completionHandler: nil) | |
// cancel the request (handled by UIApplication) | |
decisionHandler(.cancel) | |
} else { | |
// allow the request | |
decisionHandler(.allow) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment