CNWebViewController.swift
3.79 KB
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
//
// CNWebViewController.swift
// metaCode
//
// Created by zx on 2023/9/21.
//
import Foundation
import WebKit
class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{
lazy var webView: WKWebView = {
let webView = WKWebView.init(frame: CGRectMake(0, 0, Width, Height))
webView.scrollView.showsVerticalScrollIndicator = false
webView.scrollView.showsHorizontalScrollIndicator = false
webView.navigationDelegate = self
return webView
}()
var _url:URL?
var url:URL? {
get{
return _url
}
set{
_url = newValue!
if (_url != nil){
let webUrl: URL! = _url!
webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in
let userAgent = result as! String
if error == nil && userAgent.count > 0 {
if !userAgent.contains(CNLiveMCEnvironmentManager.shared.base_web_UA) {
let newUserAgent = userAgent + CNLiveMCEnvironmentManager.shared.base_web_UA
webView.customUserAgent = newUserAgent
}
}
webView.load(URLRequest.init(url:webUrl))
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
navigationBarHidden = false
self.view.addSubview(self.webView)
webView.snp.makeConstraints { make in
make.top.equalTo(get_system_nav_height())
make.left.right.bottom.equalToSuperview()
}
}
// MARK: - WKWebViewDelegate
// 页面开始加载时调用
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
}
// 当内容开始返回时调用
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("didCommit")
}
// 页面加载完成之后调用
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish")
titleName = webView.title!
}
// 页面加载失败时调用
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("didFailProvisionalNavigation")
}
// 接收到服务器跳转请求之后调用
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("didReceiveServerRedirectForProvisionalNavigation")
}
// 在收到响应后,决定是否跳转 -> 默认允许
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
print("decidePolicyFor")
//允许跳转
decisionHandler(.allow)
//不允许跳转
// decisionHandler(.cancel)
}
// 在发送请求之前,决定是否跳转 -> 默认允许
@available(iOS 13.0, *)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
if navigationAction.navigationType == .other{
//跳转Appstore
let url = URL(string: "https://apps.apple.com/cn/app/%E7%BD%91%E5%AE%B6%E5%AE%B6%E5%95%86%E5%AE%B6%E7%89%88/id1337575497")
if url == navigationAction.request.url {
if UIApplication.shared.canOpenURL(url!) { UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
}
decisionHandler(.allow, preferences)
}
}