|
|
@ -10,121 +10,82 @@ import UIKit |
|
|
|
import WebKit |
|
|
|
|
|
|
|
class WkWebViewController: UIViewController { |
|
|
|
|
|
|
|
var webView: WKWebView! |
|
|
|
var url: String? |
|
|
|
var headers: [KftcHeader]? |
|
|
|
|
|
|
|
var activityIndicator: UIActivityIndicatorView? |
|
|
|
let userContentController = WKUserContentController() |
|
|
|
|
|
|
|
override func viewDidLoad() { |
|
|
|
super.viewDidLoad() |
|
|
|
|
|
|
|
var webView: WKWebView! |
|
|
|
var url: String? |
|
|
|
var headers: [KftcHeader]? |
|
|
|
let preference = WKPreferences() |
|
|
|
preference.javaScriptEnabled = true |
|
|
|
let webConfiguration = WKWebViewConfiguration() |
|
|
|
|
|
|
|
var activityIndicator: UIActivityIndicatorView? |
|
|
|
let userContentController = WKUserContentController() |
|
|
|
userContentController.add(self, name: "test") |
|
|
|
|
|
|
|
webConfiguration.userContentController = userContentController |
|
|
|
webConfiguration.preferences = preference |
|
|
|
|
|
|
|
override func viewDidLoad() { |
|
|
|
super.viewDidLoad() |
|
|
|
|
|
|
|
|
|
|
|
let preference = WKPreferences() |
|
|
|
preference.javaScriptEnabled = true |
|
|
|
let webConfiguration = WKWebViewConfiguration() |
|
|
|
|
|
|
|
// test 1 |
|
|
|
userContentController.add(self, name: "test") |
|
|
|
|
|
|
|
// test 2 |
|
|
|
webConfiguration.userContentController = userContentController |
|
|
|
webConfiguration.preferences = preference |
|
|
|
let webView = WKWebView.init(frame: self.view.frame, configuration: webConfiguration) |
|
|
|
|
|
|
|
webView.uiDelegate = self |
|
|
|
webView.navigationDelegate = self |
|
|
|
self.view = webView |
|
|
|
self.webView = webView |
|
|
|
// other setup |
|
|
|
|
|
|
|
addCloseButton() |
|
|
|
|
|
|
|
self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) |
|
|
|
self.webView.addSubview(activityIndicator!) |
|
|
|
self.webView.isUserInteractionEnabled = true |
|
|
|
self.activityIndicator?.startAnimating() |
|
|
|
self.activityIndicator?.isHidden = false |
|
|
|
self.activityIndicator?.hidesWhenStopped = true |
|
|
|
self.webView.bringSubview(toFront: activityIndicator!) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let _url = (self.url ?? "") |
|
|
|
|
|
|
|
print(_url) |
|
|
|
|
|
|
|
if let myURL = URL(string: _url) { |
|
|
|
var myRequest = URLRequest(url: myURL) |
|
|
|
// |
|
|
|
// headers.forEach({ |
|
|
|
// myRequest.setValue($0.key ?? "", forHTTPHeaderField: $0.value ?? "") |
|
|
|
// |
|
|
|
// }) |
|
|
|
// |
|
|
|
// print(myRequest.allHTTPHeaderFields) |
|
|
|
webView.load(myRequest) |
|
|
|
} |
|
|
|
} |
|
|
|
let webView = WKWebView.init(frame: view.frame, configuration: webConfiguration) |
|
|
|
webView.uiDelegate = self |
|
|
|
webView.navigationDelegate = self |
|
|
|
|
|
|
|
private func addCloseButton() { |
|
|
|
let button = UIBarButtonItem.init(title: "Close", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self._dismiss)) |
|
|
|
self.navigationItem.rightBarButtonItem = button |
|
|
|
} |
|
|
|
view = webView |
|
|
|
self.webView = webView |
|
|
|
|
|
|
|
@objc private func _dismiss() { |
|
|
|
self.dismiss(animated: true, completion: nil) |
|
|
|
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) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
extension WkWebViewController: WKUIDelegate, WKNavigationDelegate { |
|
|
|
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { |
|
|
|
print("start") |
|
|
|
activityIndicator?.center = self.view.center |
|
|
|
// activityIndicator?.isHidden = false |
|
|
|
activityIndicator?.startAnimating() |
|
|
|
} |
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
|
|
|
// activityIndicator?.isHidden = true |
|
|
|
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: {}); |
|
|
|
} |
|
|
|
|
|
|
|
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.alert(message: body, title: nil) { |
|
|
|
self.dismiss(animated: true, completion: nil) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { |
|
|
|
print(message.name) |
|
|
|
if let body = message.body as? String { |
|
|
|
self.alert(message: body, title: nil) { |
|
|
|
self.dismiss(animated: true, completion: nil) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|