fix
杨锴
2024-08-23 adc2db9bb29e7f316c46b6de679db1522ffc9cc8
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
//
//  WebVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import WebKit
 
class WebVC: BaseVC {
 
                private var webView:WKWebView?
                private(set) var url = ""
                private(set) var htmlText = ""
                private(set) var baseUrl:URL?
                private var progressView = UIProgressView()
                private let jsCode = """
                                var meta = document.createElement('meta');"
                                "meta.name = 'viewport';"
                                "meta.content = 'width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes';"
                                "document.getElementsByTagName('head')[0].appendChild(meta);
                """
 
                public var tintColor = UIColor.blue
 
                public convenience init(url:String) {
                                self.init()
                                self.url = url
                }
 
                public convenience init(htmlText:String,baseURL:URL? = nil) {
                                self.init()
                                self.htmlText = htmlText.jq_wrapHtml()
                                self.baseUrl = baseURL
                }
 
                public override func viewDidLoad() {
                                super.viewDidLoad()
 
                                let config = WKWebViewConfiguration()
                                let userScript = WKUserScript(source: jsCode, injectionTime: .atDocumentStart, forMainFrameOnly: true)
                                let content = WKUserContentController()
                                content.addUserScript(userScript)
                                config.userContentController = content
                                webView = WKWebView(frame: CGRect.zero, configuration: config)
                                webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
                                view.addSubview(webView!)
                                webView?.snp.makeConstraints({ (make) in
                                                make.top.equalToSuperview()
                                                make.left.right.bottom.equalToSuperview()
                                })
 
                                if #available(iOS 11.0, *) {
                                                webView!.scrollView.contentInsetAdjustmentBehavior = .automatic
                                } else {
                                                self.automaticallyAdjustsScrollViewInsets = false
                                }
 
 
                                progressView.tintColor = tintColor
                                view.addSubview(progressView)
                                progressView.snp.makeConstraints { (make) in
                                                make.top.left.right.equalToSuperview()
                                                make.height.equalTo(2)
                                }
 
 
                                if !url.isEmpty {
                                                let urlRequest = URLRequest(url: URL(string: url)!)
                                                webView?.load(urlRequest)
                                }else{
                                                webView?.loadHTMLString(htmlText, baseURL: baseUrl)
                                }
                }
 
                public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
                                let value = change![NSKeyValueChangeKey(rawValue: "new")] as! Double
                                print(value)
 
 
                                progressView.setProgress(Float(value), animated: true)
                                if value == 1.0 {
                                                progressView.isHidden = true
                                }else{
                                                progressView.isHidden = false
                                }
                }
 
                deinit {
                                webView?.removeObserver(self, forKeyPath: "estimatedProgress")
                }
 
}