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.

104 lines
3.2 KiB

6 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(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
  31. webView.addSubview(activityIndicator!)
  32. webView.isUserInteractionEnabled = true
  33. activityIndicator?.startAnimating()
  34. activityIndicator?.isHidden = false
  35. activityIndicator?.hidesWhenStopped = true
  36. webView.bringSubview(toFront: activityIndicator!)
  37. if let myURL = URL(string: url ?? "") {
  38. let myRequest = URLRequest(url: myURL)
  39. webView.load(myRequest)
  40. }
  41. closeButton.title = "penny_test_close_text".localized()
  42. if let count = navigationController?.viewControllers.count,
  43. count > 1 {
  44. navigationItem.leftBarButtonItem = nil
  45. }
  46. }
  47. @IBAction func touchCloseButton(_ sender: UIBarButtonItem) {
  48. dismiss(animated: true, completion: nil)
  49. }
  50. }
  51. extension WkWebViewController: WKUIDelegate, WKNavigationDelegate {
  52. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  53. print("start")
  54. activityIndicator?.center = self.view.center
  55. activityIndicator?.startAnimating()
  56. }
  57. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  58. activityIndicator?.stopAnimating()
  59. self.navigationItem.title = webView.title
  60. }
  61. func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
  62. let alertController = UIAlertController(title: message, message: nil,
  63. preferredStyle: UIAlertController.Style.alert);
  64. alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) {
  65. _ in completionHandler()}
  66. );
  67. self.present(alertController, animated: true, completion: {});
  68. }
  69. }
  70. extension WkWebViewController: WKScriptMessageHandler {
  71. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  72. print(message.name)
  73. if let body = message.body as? String {
  74. self.alertWithOk(
  75. message: body,
  76. title: "Alert",
  77. okAction: { self.dismiss(animated: true, completion: nil) })
  78. }
  79. }
  80. }