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.

126 lines
3.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. var activityIndicator: UIActivityIndicatorView?
  15. let userContentController = WKUserContentController()
  16. @IBOutlet weak var closeButton: UIBarButtonItem!
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. let preference = WKPreferences()
  20. preference.javaScriptEnabled = true
  21. let webConfiguration = WKWebViewConfiguration()
  22. userContentController.add(self, name: "test")
  23. webConfiguration.userContentController = userContentController
  24. webConfiguration.preferences = preference
  25. let webView = WKWebView.init(frame: view.frame, configuration: webConfiguration)
  26. webView.uiDelegate = self
  27. webView.navigationDelegate = self
  28. view = webView
  29. self.webView = webView
  30. activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
  31. webView.addSubview(activityIndicator!)
  32. webView.isUserInteractionEnabled = true
  33. activityIndicator?.startAnimating()
  34. activityIndicator?.isHidden = false
  35. activityIndicator?.hidesWhenStopped = true
  36. webView.bringSubviewToFront(activityIndicator!)
  37. if let myURL = URL(string: url ?? "") {
  38. var myRequest = URLRequest(url: myURL)
  39. var header = [String: String]()
  40. headers?.forEach { model in
  41. header[model.key ?? ""] = model.value ?? ""
  42. }
  43. myRequest.allHTTPHeaderFields = header
  44. webView.load(myRequest)
  45. }
  46. closeButton.title = "penny_test_close_text".localized()
  47. if let count = navigationController?.viewControllers.count,
  48. count > 1 {
  49. navigationItem.leftBarButtonItem = nil
  50. }
  51. }
  52. override func viewWillAppear(_ animated: Bool) {
  53. super.viewWillAppear(animated)
  54. setupNormalNavigation()
  55. }
  56. @IBAction func touchCloseButton(_ sender: UIBarButtonItem) {
  57. dismiss(animated: true, completion: nil)
  58. }
  59. }
  60. extension WkWebViewController: WKUIDelegate, WKNavigationDelegate {
  61. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  62. print("start")
  63. activityIndicator?.center = self.view.center
  64. activityIndicator?.startAnimating()
  65. }
  66. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  67. activityIndicator?.stopAnimating()
  68. self.navigationItem.title = webView.title
  69. }
  70. func webView(
  71. _ webView: WKWebView,
  72. runJavaScriptAlertPanelWithMessage message: String,
  73. initiatedByFrame frame: WKFrameInfo,
  74. completionHandler: @escaping () -> Void
  75. ) {
  76. let alertController = UIAlertController(
  77. title: message,
  78. message: nil,
  79. preferredStyle: UIAlertController.Style.alert
  80. )
  81. alertController.addAction(
  82. UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) { _ in completionHandler()}
  83. )
  84. self.present(alertController, animated: true, completion: nil)
  85. }
  86. }
  87. extension WkWebViewController: WKScriptMessageHandler {
  88. func userContentController(
  89. _ userContentController: WKUserContentController,
  90. didReceive message: WKScriptMessage
  91. ) {
  92. print(message.name)
  93. if let body = message.body as? String {
  94. self.alertWithOk(
  95. message: body,
  96. title: "Alert",
  97. okAction: { self.dismiss(animated: true, completion: nil) })
  98. }
  99. }
  100. }