// // WkWebViewController.swift // GME Remit // // Created by Mac on 12/25/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import WebKit class WkWebViewController: UIViewController { var webView: WKWebView! var url: String? var headers: [KftcHeader]? var activityIndicator: UIActivityIndicatorView? let userContentController = WKUserContentController() @IBOutlet weak var closeButton: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() let preference = WKPreferences() preference.javaScriptEnabled = true let webConfiguration = WKWebViewConfiguration() userContentController.add(self, name: "test") webConfiguration.userContentController = userContentController webConfiguration.preferences = preference let webView = WKWebView.init(frame: view.frame, configuration: webConfiguration) webView.uiDelegate = self webView.navigationDelegate = self view = webView self.webView = webView activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) webView.addSubview(activityIndicator!) webView.isUserInteractionEnabled = true activityIndicator?.startAnimating() activityIndicator?.isHidden = false activityIndicator?.hidesWhenStopped = true webView.bringSubview(toFront: activityIndicator!) if let myURL = URL(string: url ?? "") { let myRequest = URLRequest(url: myURL) webView.load(myRequest) } closeButton.title = "penny_test_close_text".localized() if let count = navigationController?.viewControllers.count, count > 1 { navigationItem.leftBarButtonItem = nil } } @IBAction func touchCloseButton(_ sender: UIBarButtonItem) { dismiss(animated: true, completion: nil) } } extension WkWebViewController: WKUIDelegate, WKNavigationDelegate { func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { print("start") activityIndicator?.center = self.view.center activityIndicator?.startAnimating() } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { activityIndicator?.stopAnimating() self.navigationItem.title = webView.title } func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { let alertController = UIAlertController(title: message, message: nil, preferredStyle: UIAlertController.Style.alert); alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) { _ in completionHandler()} ); self.present(alertController, animated: true, completion: {}); } } extension WkWebViewController: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { print(message.name) if let body = message.body as? String { self.alertWithOk( message: body, title: "Alert", okAction: { self.dismiss(animated: true, completion: nil) }) } } }