iOS 使用WKWebView

页面禁用长按事件

方式一

禁用长按选择

1
2
3
4
5
6
7
8
9
10
11
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//页面加载完成时
webView.evaluateJavaScript(
"document.documentElement.style.webkitUserSelect='none';",
completionHandler: nil
);
webView.evaluateJavaScript(
"document.documentElement.style.webkitTouchCallout='none';",
completionHandler: nil
)
}

方式二

完全禁用长按事件

1
2
3
4
5
6
7
8
9
10
11
12
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)
}
}
}
}
}

常用代理方法

  • WKUIDelegate
  • WKNavigationDelegate
  • WKScriptMessageHandler
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
}

//MARK:-WKUIDelegate
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
// 在JS端调用alert函数时,会触发此代理方法。
// JS端调用alert时所传的数据可以通过message拿到
// 在原生得到结果后,需要回调JS,是通过completionHandler回调

self.showNoticeText(message, time: 1.2) {
completionHandler()
}
}


func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
// JS端调用confirm函数时,会触发此方法
// 通过message可以拿到JS端所传的数据
// 在iOS端显示原生alert得到YES/NO后
// 通过completionHandler回调给JS端
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) {
// JS端调用prompt函数时,会触发此方法
// 要求输入一段文本
// 在原生输入得到文本内容后,通过completionHandler回调给JS
let alertTextField = UIAlertController.init(title: "请输入", message: "JS调用输入框", preferredStyle: UIAlertController.Style.alert)
alertTextField.addTextField {
(textField:UITextField) in
//设置textField相关属性
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)
}

//MARK:-WKNavigationDelegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//页面开始加载,可在这里给用户loading提示
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)
}

//MARK:-WKScriptMessageHandler
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
//h5给端传值的内容,可在这里实现h5与原生的交互时间
let messageDic = message.body
print(messageDic)

}