-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyWebViewController.swift
102 lines (82 loc) · 3.85 KB
/
MyWebViewController.swift
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
//
// MyWebViewController.swift
// midtrans-snap-WebView-sample
//
// Created by Zaki Ibrahim on 14/09/21.
//
import UIKit
import WebKit
class MyWebViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var selectedName: String = ""
@IBOutlet weak var webView: WKWebView!
private var activityIndicatorContainer: UIView!
private var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
var urlS : URL
// Get URL from text field, if empty get Snap URL from Sample web
if !selectedName.isEmpty {
urlS = URL(string: selectedName)!
} else {
urlS = URL(string: "https://sample-demo-dot-midtrans-support-tools.et.r.appspot.com/snap-redirect")!
}
webView.navigationDelegate = self
view.addSubview(webView)
webView.load(URLRequest(url: urlS))
}
// WKWebView Configuration
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
// detect these specified deeplinks and e_money simulator to be handled by OS
if url!.absoluteString.hasPrefix("https://simulator.sandbox.midtrans.com/gopay/partner/")
|| url!.absoluteString.hasPrefix("https://simulator.sandbox.midtrans.com/shopeepay/")
|| url!.absoluteString.hasPrefix("shopeeid://")
|| url!.absoluteString.hasPrefix("gojek://")
|| url!.absoluteString.hasPrefix("//wsa.wallet.airpay.co.id/") {
decisionHandler(.cancel)
UIApplication.shared.open(url!)
// any other url will be loaded normally by the WebView
} else {
decisionHandler(.allow)
}
}
// Loading animator
fileprivate func setActivityIndicator() {
activityIndicatorContainer = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
activityIndicatorContainer.center.x = webView.center.x
activityIndicatorContainer.center.y = webView.center.y - 44
activityIndicatorContainer.backgroundColor = UIColor.black
activityIndicatorContainer.alpha = 0.8
activityIndicatorContainer.layer.cornerRadius = 10
// Configure the activity indicator
activityIndicator = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.style = UIActivityIndicatorView.Style.whiteLarge
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorContainer.addSubview(activityIndicator)
webView.addSubview(activityIndicatorContainer)
// Constraints
activityIndicator.centerXAnchor.constraint(equalTo: activityIndicatorContainer.centerXAnchor).isActive = true
activityIndicator.centerYAnchor.constraint(equalTo: activityIndicatorContainer.centerYAnchor).isActive = true
}
// Helper function to control activityIndicator's animation
fileprivate func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
} else {
activityIndicator.stopAnimating()
activityIndicatorContainer.removeFromSuperview()
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.showActivityIndicator(show: false)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// Set the indicator everytime webView started loading
self.setActivityIndicator()
self.showActivityIndicator(show: true)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
self.showActivityIndicator(show: false)
}
}