1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| lazy var webConfiguration: WKWebViewConfiguration = { let configuration = WKWebViewConfiguration.init() let preferences = WKPreferences.init() preferences.javaScriptCanOpenWindowsAutomatically = true configuration.preferences = preferences configuration.userContentController = self.webUserContentController return configuration }()
lazy var webUserContentController: WKUserContentController = { let userConetentController = WKUserContentController.init() userConetentController.add(self, name: "webViewApp") return userConetentController }()
func setupWebView() { myWebView = WKWebView.init(frame: self.webouterView.bounds, configuration: webConfiguration) self.webouterView.addSubview(myWebView) myWebView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth
let web_url = URL.init(string: weburl) myWebView.load(URLRequest.init(url: web_url!)) myWebView.navigationDelegate = self myWebView.uiDelegate = self myWebView.scrollView.bounces = false;
}
override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) { myWebView.frame = self.webouterView.bounds }
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if(!(navigationAction.targetFrame?.isMainFrame ?? true)){ webView.load(navigationAction.request) } return nil }
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
self.showNoticeText(message, time: 1.2) { completionHandler() } }
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) { let alertView = UIAlertController.init(title: "提示", message:message, preferredStyle: UIAlertController.Style.alert) let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel) { (action:UIAlertAction) in completionHandler(false) } alertView.addAction(cancelAction) let okAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default) { (action:UIAlertAction) in completionHandler(true) } alertView.addAction(okAction) self.present(alertView, animated: true, completion: nil) }
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) { let alertTextField = UIAlertController.init(title: "请输入", message: "JS调用输入框", preferredStyle: UIAlertController.Style.alert) alertTextField.addTextField { (textField:UITextField) in textField.textColor = UIColor.red } let okAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.destructive) { (action:UIAlertAction) in completionHandler(alertTextField.textFields?.last?.text) } alertTextField.addAction(okAction) self.present(alertTextField, animated: true, completion: nil) }
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { self.showNoticeWait(text: "加载中..."); }
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { self.clearAllNotice(); }
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { for subview in webView.scrollView.subviews { if(subview.gestureRecognizers != nil){ for longPress in subview.gestureRecognizers!{ if(longPress is UILongPressGestureRecognizer){ subview.removeGestureRecognizer(longPress) } } } } }
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { }
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { }
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { decisionHandler(WKNavigationActionPolicy.allow) }
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { decisionHandler(WKNavigationResponsePolicy.allow) }
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { let messageDic = message.body print(messageDic)
}
|