150 lines
5.1 KiB
Swift
150 lines
5.1 KiB
Swift
//
|
|
// BRWebView.swift
|
|
// BeeReel
|
|
//
|
|
// Created by 湖南秦九 on 2025/7/22.
|
|
//
|
|
|
|
import UIKit
|
|
@preconcurrency import WebKit
|
|
|
|
//MARK:-------------- VPWebViewDelegate --------------
|
|
@objc protocol BRWebViewDelegate: NSObjectProtocol {
|
|
|
|
@objc optional func br_webView(_ webView: BRWebView, shouldStartLoadWith navigationAction: WKNavigationAction) -> Bool
|
|
|
|
@objc optional func br_webViewDidStartLoad(_ webView: BRWebView)
|
|
|
|
@objc optional func br_webViewDidFinishLoad(_ webView: BRWebView)
|
|
|
|
@objc optional func br_webView(_ webView: BRWebView, didFailLoadWithError error: Error)
|
|
|
|
///进度
|
|
@objc optional func br_webView(webView: BRWebView, didChangeProgress progress: CGFloat)
|
|
///标题
|
|
@objc optional func br_webView(webView: BRWebView, didChangeTitle title: String)
|
|
|
|
///web交互用
|
|
@objc optional func br_userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
|
|
|
|
}
|
|
|
|
class BRWebView: WKWebView {
|
|
|
|
weak var delegate: BRWebViewDelegate?
|
|
|
|
private(set) var scriptMessageHandlerArray: [String] = []
|
|
|
|
|
|
deinit {
|
|
self.removeObserver(self, forKeyPath: "estimatedProgress")
|
|
self.removeObserver(self, forKeyPath: "title")
|
|
|
|
}
|
|
|
|
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
|
|
super.init(frame: frame, configuration: configuration)
|
|
addScriptMessageHandler()
|
|
_setupInit()
|
|
}
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
private func _setupInit() {
|
|
|
|
self.isOpaque = false
|
|
self.navigationDelegate = self
|
|
self.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
|
|
self.addObserver(self, forKeyPath: "title", options: .new, context: nil)
|
|
|
|
}
|
|
|
|
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
|
if object as? BRWebView == self {
|
|
if keyPath == "estimatedProgress", let progress = change?[NSKeyValueChangeKey.newKey] as? CGFloat {
|
|
self.delegate?.br_webView?(webView: self, didChangeProgress: progress)
|
|
} else if keyPath == "title", let title = change?[NSKeyValueChangeKey.newKey] as? String {
|
|
self.delegate?.br_webView?(webView: self, didChangeTitle: title)
|
|
}
|
|
}
|
|
}
|
|
|
|
func load(urlStr: String) {
|
|
guard let url = URL(string: urlStr) else { return }
|
|
let request = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 30)
|
|
self.load(request)
|
|
}
|
|
|
|
func removeScriptMessageHandler() {
|
|
self.scriptMessageHandlerArray.forEach{
|
|
configuration.userContentController.removeScriptMessageHandler(forName: $0)
|
|
}
|
|
}
|
|
func addScriptMessageHandler() {
|
|
self.scriptMessageHandlerArray.forEach{
|
|
configuration.userContentController.add(YYTextWeakProxy(target: self) as! WKScriptMessageHandler, name: $0)
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//MARK:-------------- WKNavigationDelegate --------------
|
|
extension BRWebView: WKNavigationDelegate {
|
|
|
|
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
|
|
|
|
decisionHandler(.allow);
|
|
}
|
|
|
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
|
if let url = navigationAction.request.url,
|
|
url.scheme != "http",
|
|
url.scheme != "https"
|
|
{
|
|
UIApplication.shared.open(url)
|
|
decisionHandler(.cancel)
|
|
return
|
|
}
|
|
|
|
if let result = self.delegate?.br_webView?(self, shouldStartLoadWith: navigationAction) {
|
|
if result {
|
|
decisionHandler(.allow)
|
|
} else {
|
|
decisionHandler(.cancel)
|
|
}
|
|
} else {
|
|
decisionHandler(.allow)
|
|
}
|
|
}
|
|
|
|
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
|
|
self.delegate?.br_webViewDidStartLoad?(self)
|
|
}
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
self.delegate?.br_webViewDidFinishLoad?(self)
|
|
}
|
|
|
|
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
self.delegate?.br_webView?(self, didFailLoadWithError: error)
|
|
}
|
|
|
|
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
self.delegate?.br_webView?(self, didFailLoadWithError: error)
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//MARK:-------------- WKScriptMessageHandler --------------
|
|
extension BRWebView: WKScriptMessageHandler {
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
self.delegate?.br_userContentController?(userContentController, didReceive: message)
|
|
}
|
|
|
|
}
|