相关链接
代码示例
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("message ****** \(message.body)")
}
var webViewOne: WKWebView!
var contentController: WKUserContentController = WKUserContentController()
override func loadView() {
let jsStr = ""
let userScript = WKUserScript(source: jsStr, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)
let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController = userContentController
webViewOne = WKWebView(frame: .zero, configuration: webConfiguration)
webViewOne.uiDelegate = self
webViewOne.navigationDelegate = self
webViewOne.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
self.view = webViewOne
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://tianji.rong360.com/tianjiwapreport/login?data=nD%2BRaNTUBXKJrM1QPpIetIpWI2iU5DejAjUo%2FRXJHMv%2BW13DSPQahuoMWVB1WxLBSyAwG7bcxRVgUE003PjpCrXD5DFxfthePEMRS7jd%2F4Wc%2BkmOEondu1Ckyn9lG9hhYaBobcA%2BanEaWnih7OLhtRwm%2FNT8eXMhWwPlmNvix0SWHY%2F3%2FFwxqaA1qNtQCTt7iYprTYXRRvdSr5ToKleZwO1aFQYPTGhFqLXe2sW2fi6oKE%2FZKdVQ2ovlICIo7qUS")
let myRequest = URLRequest(url: myURL!)
webViewOne.load(myRequest)
contentController.add(self, name: "callbackHandler")
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("页面开始加载")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("内容开始返回")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("页面加载结束 ******** \(webView.url?.absoluteString)")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("网页加载失败")
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("收到服务器跳转请求之后调用")
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
print("************** \(webView.url)")
decisionHandler(WKNavigationResponsePolicy.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let jsString = "var list = document.getElementsByClassName('icon-icon-arrowleft')[0];list.onclick=function(){alert('点击了返回事件')}"
webView.evaluateJavaScript(jsString) { (any, error) in
}
print(webViewOne.backForwardList.backList.count)
print(navigationAction.request.url!.absoluteString)
decisionHandler(WKNavigationActionPolicy.allow)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
print("************** 1")
return webView
}
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alertVC = UIAlertController.init(title: "提示", message: message, preferredStyle: .alert)
alertVC.addAction(UIAlertAction.init(title: "确定", style: .default, handler: { (action) in
completionHandler()
}))
self.present(alertVC, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
print("调用了确认框")
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
print("处理网页js的文本输入")
}
@available(iOS 9.0, *)
func webViewDidClose(_ webView: WKWebView) {
print("关闭了")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
if object as? WKWebView == webViewOne {
print("进度条 ****** \(webViewOne.estimatedProgress)")
}
}
}
deinit {
self.removeObserver(self, forKeyPath: "estimatedProgress")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}