|
|
|
@@ -7,12 +7,14 @@
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import WebKit
|
|
|
|
|
import MJExtension
|
|
|
|
|
class WebViewWeakScriptMessage: NSObject, WKScriptMessageHandler {
|
|
|
|
|
|
|
|
|
|
private(set) weak var target: WKScriptMessageHandler?
|
|
|
|
|
|
|
|
|
|
required init(target: WKScriptMessageHandler?) {
|
|
|
|
|
self.target = target
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -30,8 +32,20 @@ class WebViewController: BaseViewController {
|
|
|
|
|
|
|
|
|
|
private(set) var progressView = UIProgressView()
|
|
|
|
|
|
|
|
|
|
private lazy var userContentController = WKUserContentController()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private lazy var userContentController:WKUserContentController = {
|
|
|
|
|
let scriptMessage = WebViewWeakScriptMessage(target: self);
|
|
|
|
|
|
|
|
|
|
let _userContentController = WKUserContentController()
|
|
|
|
|
_userContentController.add(self, name: "getUid")
|
|
|
|
|
_userContentController.add(self, name: "getDeviceId")
|
|
|
|
|
_userContentController.add(self, name: "getTicket")
|
|
|
|
|
_userContentController.add(self, name: "getDeviceInfo")
|
|
|
|
|
_userContentController.add(self, name: "initShowNav")
|
|
|
|
|
_userContentController.add(self, name: "closeWebView")
|
|
|
|
|
_userContentController.add(self, name: "cancelAccount")
|
|
|
|
|
return _userContentController
|
|
|
|
|
}()
|
|
|
|
|
private var callbacks: [String: [OnReceiveMessage]] = [:]
|
|
|
|
|
|
|
|
|
|
private(set) weak var navigationDelegate: WKNavigationDelegate?
|
|
|
|
@@ -47,7 +61,7 @@ class WebViewController: BaseViewController {
|
|
|
|
|
self.isHalf = isHalf
|
|
|
|
|
if var url = url {
|
|
|
|
|
if !url.hasPrefix("http") {
|
|
|
|
|
url = "\(H5_URL)/\(url)"
|
|
|
|
|
url = "\(AppKeys.H5_URL)/\(url)"
|
|
|
|
|
}
|
|
|
|
|
self.url = url
|
|
|
|
|
}
|
|
|
|
@@ -59,7 +73,9 @@ class WebViewController: BaseViewController {
|
|
|
|
|
loadSubViews()
|
|
|
|
|
loadWebView()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deinit{
|
|
|
|
|
print("1111")
|
|
|
|
|
}
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
addObserve()
|
|
|
|
@@ -157,8 +173,11 @@ class WebViewController: BaseViewController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webview.load(URLRequest(url: theUrl))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func reloadWebViewWithUrl(_ url: String) {
|
|
|
|
|
self.url = url
|
|
|
|
|
loadWebView()
|
|
|
|
@@ -170,8 +189,18 @@ class WebViewController: BaseViewController {
|
|
|
|
|
configuration.allowsInlineMediaPlayback = true
|
|
|
|
|
configuration.allowsPictureInPictureMediaPlayback = true
|
|
|
|
|
configuration.selectionGranularity = .character
|
|
|
|
|
configuration.userContentController = userContentController
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cookie = "uid=\(AuthManager.userUid)"
|
|
|
|
|
let cookieScript = WKUserScript.init(source: "document.cookie = '\(cookie)';", injectionTime: .atDocumentStart, forMainFrameOnly: false)
|
|
|
|
|
self.userContentController.addUserScript(cookieScript)
|
|
|
|
|
|
|
|
|
|
let scale = "$('meta[name=description]').remove(); $('head').append( '<meta name=\"viewport\" content=\"width=device-width, initial-scale=1,user-scalable=no\">' );"
|
|
|
|
|
|
|
|
|
|
let scaleScript = WKUserScript.init(source: scale, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
|
|
|
|
|
self.userContentController.addUserScript(scaleScript)
|
|
|
|
|
configuration.userContentController = self.userContentController
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let preferences = WKPreferences()
|
|
|
|
|
if #available(iOS 14, *) {
|
|
|
|
|
let webpagePreferences = WKWebpagePreferences()
|
|
|
|
@@ -185,6 +214,18 @@ class WebViewController: BaseViewController {
|
|
|
|
|
configuration.preferences = preferences
|
|
|
|
|
let webview = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
|
|
|
|
|
webview.navigationDelegate = self
|
|
|
|
|
webview.evaluateJavaScript("navigator.userAgent") {[weak self] res, error in
|
|
|
|
|
guard let self = self else{return}
|
|
|
|
|
if let text = res as? NSString{
|
|
|
|
|
if !text.contains("tutuAppIos erbanAppIos"){
|
|
|
|
|
let newText = (text as String) + "tutuAppIos erbanAppIos"
|
|
|
|
|
let dic = ["UserAgent":newText]
|
|
|
|
|
UserDefaults.standard.register(defaults: dic)
|
|
|
|
|
UserDefaults.standard.synchronize()
|
|
|
|
|
self.webview.customUserAgent = newText
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return webview
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -200,10 +241,54 @@ extension WebViewController: WKScriptMessageHandler {
|
|
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
|
|
//TODO: 拦截一些h5的事件
|
|
|
|
|
webview.evaluateJavaScript("document.location.href") {[weak self] res, error in
|
|
|
|
|
guard let self = self else{return}
|
|
|
|
|
if let curUrl = res{
|
|
|
|
|
if message.name == "getUid"{
|
|
|
|
|
let uid = "getMessage(\"uid\",\(AuthManager.userUid))"
|
|
|
|
|
self.webview.evaluateJavaScript(uid) { other, error in
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}else if message.name == "getTicket"{
|
|
|
|
|
let ticket = "getMessage(\"ticket\",\"\(AuthManager.ticket)\")"
|
|
|
|
|
self.webview.evaluateJavaScript(ticket) { other, error in
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}else if message.name == "getDeviceId"{
|
|
|
|
|
let deviceId = "getMessage(\"deviceId\",\"\(APPUtils.currentDeveiceId)\")"
|
|
|
|
|
self.webview.evaluateJavaScript(deviceId) { other, error in
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}else if message.name == "getDeviceInfo"{
|
|
|
|
|
let info = NSDictionary(dictionary: YMNetworkHelper.share.baseParameters)
|
|
|
|
|
if let infoText = info.mj_JSONString() {
|
|
|
|
|
let text = "getMessage(\"deviceInfo\",\(infoText))"
|
|
|
|
|
self.webview.evaluateJavaScript(text) { other, error in
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else if message.name == "initShowNav"{
|
|
|
|
|
if let body = message.body as? Int,body == 0{
|
|
|
|
|
if self.navigationController != nil{
|
|
|
|
|
self.navigationController?.setNavigationBarHidden(true, animated: false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else if message.name == "closeWebView"{
|
|
|
|
|
if let body = message.body as? Int,body == 0{
|
|
|
|
|
if self.navigationController != nil{
|
|
|
|
|
self.navigationController?.popViewController(animated: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else if message.name == "cancelAccount"{
|
|
|
|
|
AuthViewModel.authVM.logout()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension WebViewController: WKNavigationDelegate {
|
|
|
|
|
extension WebViewController: WKNavigationDelegate{
|
|
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
|
|
|
|
|
progressView.alpha = 1
|
|
|
|
@@ -224,4 +309,5 @@ extension WebViewController: WKNavigationDelegate {
|
|
|
|
|
}
|
|
|
|
|
navigationDelegate?.webView?(webView, didFailProvisionalNavigation: navigation, withError: error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|