-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWKWebViewController.swift
128 lines (99 loc) · 4.84 KB
/
WKWebViewController.swift
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
//
// WKWebViewController.swift
// iOSAppDemo
//
// Created by WilliamYang on 2022/1/2.
//
import WebKit
import UIKit
class WKWebViewController: UIViewController, WKScriptMessageHandler, WKUIDelegate {
var wkWebView: WKWebView?
var progressView: UIProgressView?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "WKWebView Demo"
let configuration = WKWebViewConfiguration()
// 设置进程池,一般不需要设置,在使用多个 WKWebView 实例时会使用到
configuration.processPool = WKProcessPool()
// 设置偏好
let prefrence = WKPreferences()
// 设置网页中最小字体
prefrence.minimumFontSize = 0
// 支持 js,在 iOS 14 中已弃用
// prefrence.javaScriptEnabled = true
// 设置允许不经过交互,由 js 自动打开窗口
prefrence.javaScriptCanOpenWindowsAutomatically = true
let pagePrefrence = WKWebpagePreferences()
// 支持 js
pagePrefrence.allowsContentJavaScript = true
configuration.preferences = prefrence
configuration.defaultWebpagePreferences = pagePrefrence
// 设置 js 交互
let contentController = WKUserContentController()
// 设置代理并注册要被 js 调用的原生方法的事件名称
contentController.add(self, name: "callNativeMethod")
// 向网页注入 js 代码
let jsStr = "function jsFunc() { window.webkit.messageHandlers.nativeMethod.postMessage({\"key\":\"value\"}) }; jsFunc()"
let script = WKUserScript(source: jsStr, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: false)
// 进行注入
contentController.addUserScript(script)
configuration.userContentController = contentController
wkWebView = WKWebView(frame: self.view.frame, configuration: configuration)
self.view.addSubview(wkWebView!)
let url = URL(string: "https://github.com/WeiLianYang/iOSAppDemo")
let request = URLRequest(url: url!)
wkWebView?.load(request)
// 调用 js
wkWebView?.evaluateJavaScript("jsFunc", completionHandler: { any, error in
print("error: \(error.debugDescription)")
})
// 创建进度条控件
progressView = UIProgressView(frame: CGRect(x: 0, y: 140, width: self.view.frame.size.width, height: 10))
progressView?.tintColor = UIColor.green
progressView?.progress = 0
self.view.addSubview(progressView!)
// 添加进度条监听
wkWebView?.addObserver(self, forKeyPath: "estimatedProgress", options: NSKeyValueObservingOptions.new, context: nil)
}
// 监听加载进度
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "estimatedProgress") {
let progress = wkWebView!.estimatedProgress
print("progress: \(progress)")
progressView?.progress = Float(progress)
if (progress == 1.0) {
progressView?.isHidden = true
}
}
}
// js 调用 原生方法回调处
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("body: ", message.body, ", name: ", message.name)
}
// 当网页视图被创建时回调
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
return wkWebView
}
// 当网页视图被关闭时回调
func webViewDidClose(_ webView: WKWebView) {
}
// js 弹出 alert 警告框时回调
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo) async {
}
// js 弹出 confirm 确认框时回调
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo) async -> Bool {
return true
}
// js 弹出输入框时回调
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo) async -> String? {
return nil
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}