You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.5 KiB

6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // WkWebViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Mac on 12/25/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. class WkWebViewController: UIViewController {
  11. var webView: WKWebView!
  12. var url: String?
  13. var headers: [KftcHeader]?
  14. weak var delegate: NewRegisterDelegate?
  15. var activityIndicator: UIActivityIndicatorView?
  16. let userContentController = WKUserContentController()
  17. @IBOutlet weak var closeButton: UIBarButtonItem!
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. let preference = WKPreferences()
  21. preference.javaScriptEnabled = true
  22. preference.javaScriptCanOpenWindowsAutomatically = true
  23. let webConfiguration = WKWebViewConfiguration()
  24. userContentController.add(self, name: "test")
  25. webConfiguration.userContentController = userContentController
  26. webConfiguration.preferences = preference
  27. let webView = WKWebView.init(frame: view.frame, configuration: webConfiguration)
  28. webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  29. webView.allowsBackForwardNavigationGestures = true
  30. webView.uiDelegate = self
  31. webView.navigationDelegate = self
  32. view = webView
  33. self.webView = webView
  34. setUserAgentForCloudCert(webView: self.webView)
  35. activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
  36. webView.addSubview(activityIndicator!)
  37. webView.isUserInteractionEnabled = true
  38. activityIndicator?.startAnimating()
  39. activityIndicator?.isHidden = false
  40. activityIndicator?.hidesWhenStopped = true
  41. webView.bringSubviewToFront(activityIndicator!)
  42. if let myURL = URL(string: url ?? "") {
  43. var myRequest = URLRequest(url: myURL)
  44. var header = [String: String]()
  45. headers?.forEach { model in
  46. header[model.key ?? ""] = model.value ?? ""
  47. }
  48. myRequest.allHTTPHeaderFields = header
  49. webView.load(myRequest)
  50. }
  51. closeButton.title = "penny_test_close_text".localized()
  52. if let count = navigationController?.viewControllers.count,
  53. count > 1 {
  54. navigationItem.leftBarButtonItem = nil
  55. }
  56. }
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. setupNormalNavigation()
  60. }
  61. @IBAction func touchCloseButton(_ sender: UIBarButtonItem) {
  62. dismiss(animated: true, completion: nil)
  63. }
  64. private func setUserAgentForCloudCert(webView: WKWebView) {
  65. webView.evaluateJavaScript("navigator.userAgent") { (userAgent, _) in
  66. guard let userAgent = userAgent as? String else { return }
  67. webView.customUserAgent = "\(userAgent) yessign/wv/i-Lzo41VQc1vdRqXJD5Cw4;1.0"
  68. }
  69. }
  70. @IBAction func touchCertTest(_ sender: Any) {
  71. if let myURL = URL(string: "https://testapi.openbanking.or.kr/oauth/2.0/authorize?auth_type=0&response_type=code&client_id=KxvHs6w5RLvByYmLrj3vCeD9Y9fWknzvLHetOhUE&edit_option=on&scope=inquiry&redirect_uri=https://www.kftc.or.kr&state=abcdefghijklmnopqrstuvwxyz123456&lang=kor") {
  72. let myRequest = URLRequest(url: myURL)
  73. webView.load(myRequest)
  74. }
  75. }
  76. }
  77. extension WkWebViewController: WKUIDelegate, WKNavigationDelegate {
  78. func webView(
  79. _ webView: WKWebView,
  80. createWebViewWith configuration: WKWebViewConfiguration,
  81. for navigationAction: WKNavigationAction,
  82. windowFeatures: WKWindowFeatures
  83. ) -> WKWebView? {
  84. if navigationAction.targetFrame == nil {
  85. var urlComponent = URLComponents()
  86. let url = navigationAction.request.url
  87. urlComponent.scheme = url?.scheme
  88. urlComponent.host = url?.host
  89. urlComponent.path = url?.path ?? ""
  90. if urlComponent.url?.absoluteString.range(of: "yessign.or.kr/open/html/v2/") != nil {
  91. let popupWebView = WKWebView(
  92. frame: CGRect(x: -1, y: -1, width: 1, height: 1),
  93. configuration: configuration
  94. )
  95. popupWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  96. setUserAgentForCloudCert(webView: popupWebView)
  97. popupWebView.uiDelegate = self
  98. popupWebView.navigationDelegate = self
  99. view.addSubview(popupWebView)
  100. return popupWebView
  101. }
  102. }
  103. if navigationAction.request.url?.absoluteString.range(of: "yessign.or.kr") != nil {
  104. guard let url = navigationAction.request.url else { return nil }
  105. if UIApplication.shared.canOpenURL(url) {
  106. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  107. }
  108. }
  109. return nil
  110. }
  111. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  112. print("start")
  113. activityIndicator?.center = self.view.center
  114. activityIndicator?.startAnimating()
  115. }
  116. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  117. activityIndicator?.stopAnimating()
  118. self.navigationItem.title = webView.title
  119. }
  120. func webView(
  121. _ webView: WKWebView,
  122. runJavaScriptAlertPanelWithMessage message: String,
  123. initiatedByFrame frame: WKFrameInfo,
  124. completionHandler: @escaping () -> Void
  125. ) {
  126. let alertController = UIAlertController(
  127. title: message,
  128. message: nil,
  129. preferredStyle: UIAlertController.Style.alert
  130. )
  131. alertController.addAction(
  132. UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) { _ in completionHandler()}
  133. )
  134. self.present(alertController, animated: true, completion: nil)
  135. }
  136. func webView(
  137. _ webView: WKWebView,
  138. didReceive challenge: URLAuthenticationChallenge,
  139. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
  140. ) {
  141. guard let trust = challenge.protectionSpace.serverTrust else { return }
  142. let credential = URLCredential(trust: trust)
  143. completionHandler(.useCredential, credential)
  144. }
  145. }
  146. extension WkWebViewController: WKScriptMessageHandler {
  147. func userContentController(
  148. _ userContentController: WKUserContentController,
  149. didReceive message: WKScriptMessage
  150. ) {
  151. print(message.name)
  152. if let body = message.body as? String, body.lowercased().contains("successful") {
  153. guard let delegate = delegate else {
  154. self.alert(type: .success, message: "Auto-Debit account register is complete.") {
  155. self.dismiss(animated: true, completion: nil)
  156. }
  157. return
  158. }
  159. } else {
  160. alert(type: .error, message: "An error occurred.\nPlease contact customer service team.") {
  161. self.dismiss(animated: true)
  162. }
  163. }
  164. }
  165. }