|
@@ -0,0 +1,183 @@
|
|
|
+//
|
|
|
+// JDActivityWebViewController.swift
|
|
|
+// Koala
|
|
|
+//
|
|
|
+// Created by mac on 2024/8/23.
|
|
|
+// Copyright © 2024 Koala. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import SafariServices
|
|
|
+import UIKit
|
|
|
+import WebKit
|
|
|
+
|
|
|
+class JDActivityWebViewController: BaseViewController {
|
|
|
+
|
|
|
+ var webView: WKWebView!
|
|
|
+ var config: WKWebViewConfiguration!
|
|
|
+
|
|
|
+ var webReady: Bool! = false
|
|
|
+
|
|
|
+ var url: String! = ""
|
|
|
+ var navTitle : String = switchLanguage("活动详情")
|
|
|
+
|
|
|
+ /// 添加进度条
|
|
|
+ lazy var progressView: UIProgressView = {
|
|
|
+ self.progressView = UIProgressView(frame: CGRect(x: 0, y: TopHeight, width: SCREEN_WIDTH, height: 2))
|
|
|
+ self.progressView.tintColor = MainBackGroundColor // 进度条颜色
|
|
|
+ self.progressView.trackTintColor = ViewBorderColor // 进度条背景色
|
|
|
+ return self.progressView
|
|
|
+ }()
|
|
|
+
|
|
|
+ override func viewDidLoad() {
|
|
|
+ super.viewDidLoad()
|
|
|
+ myNavigationBar.title_Label.text = navTitle
|
|
|
+// self.myNavigationBar.title_Label.textColor = .white
|
|
|
+// self.myNavigationBar.left_Button.setImage(_image("Base_White_Back&Base_White_Back"), for: .normal)
|
|
|
+// self.myNavigationBar.backgroundColor = .clear
|
|
|
+// self.myNavigationBar.back_ImageView.isHidden = true
|
|
|
+// self.myNavigationBar.left_ImageView.isHidden = true
|
|
|
+// self.myNavigationBar.left_SuperView.backgroundColor = .clear
|
|
|
+
|
|
|
+// switch GlobalSingle.sharedInstance.currentLan {
|
|
|
+// case .EnglishLanguage:
|
|
|
+// url = "https://m.jdnx2.com/en/share?flag=app&token="+OperationalUserInfoData.getToken()
|
|
|
+// case .TraditionalLanguage:
|
|
|
+// url = "https://m.jdnx2.com/zh-TW/share?flag=app&language=zh_TW&token="+OperationalUserInfoData.getToken()
|
|
|
+// case .ChineseLanguage:
|
|
|
+// url = "https://m.jdnx2.com/zh-CN/share?flag=app&language=zh_CN&token="+OperationalUserInfoData.getToken()
|
|
|
+// default:
|
|
|
+//// vc.url = JoinENUrl+OperationalUserInfoData.getUserInfoDataModel().uid+"?flag=app"
|
|
|
+// url = "https://m.jdnx2.com/en/share?flag=app&language=en&token="+OperationalUserInfoData.getToken()
|
|
|
+// }
|
|
|
+
|
|
|
+ print(url)
|
|
|
+
|
|
|
+// url = "https://m.jdnx.com/share?flag=app&token="+OperationalUserInfoData.getToken()
|
|
|
+
|
|
|
+ if url.count > 0 {
|
|
|
+ var charSet = CharacterSet.urlQueryAllowed
|
|
|
+ charSet.insert(charactersIn: "#")
|
|
|
+ if let u = URL(string: url.addingPercentEncoding(withAllowedCharacters: charSet)!) {
|
|
|
+ let js = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
|
|
|
+ let userScript = WKUserScript(source: js, injectionTime: .atDocumentStart, forMainFrameOnly: false)
|
|
|
+ let config = WKWebViewConfiguration()
|
|
|
+ config.userContentController.addUserScript(userScript)
|
|
|
+ config.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
|
|
|
+ config.websiteDataStore = WKWebsiteDataStore.default()
|
|
|
+ // webView = WKWebView(frame: CGRect(x: 0, y: TopHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-TopHeight-KBottomMargin), configuration: config)
|
|
|
+
|
|
|
+ webView = WKWebView(frame: .zero, configuration: config)
|
|
|
+
|
|
|
+ webView.backgroundColor = .clear
|
|
|
+ webView.isOpaque = false
|
|
|
+ webView.navigationDelegate = self
|
|
|
+ view.addSubview(webView)
|
|
|
+
|
|
|
+ webView.load(URLRequest(url: u))
|
|
|
+ webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
|
|
|
+
|
|
|
+ view.bringSubviewToFront(myNavigationBar)
|
|
|
+// return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ view.addSubview(progressView)
|
|
|
+ view.bringSubviewToFront(progressView) // 将进度条至于最顶层
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewWillLayoutSubviews() {
|
|
|
+ super.viewWillLayoutSubviews()
|
|
|
+
|
|
|
+// bottomView.snp.makeConstraints { make in
|
|
|
+// make.left.right.bottom.equalTo(self.view)
|
|
|
+// make.height.equalTo(136)
|
|
|
+// }
|
|
|
+
|
|
|
+ webView.snp.makeConstraints { make in
|
|
|
+ make.left.right.equalToSuperview()
|
|
|
+ make.top.equalTo(self.myNavigationBar.snp.bottom)
|
|
|
+ make.bottom.equalTo(self.view.snp.bottom)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension JDActivityWebViewController: WKScriptMessageHandler {
|
|
|
+ override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
|
|
|
+ // 加载进度条
|
|
|
+ if keyPath == "estimatedProgress" {
|
|
|
+ progressView.alpha = 1.0
|
|
|
+ progressView.setProgress(Float(webView.estimatedProgress), animated: true)
|
|
|
+ if (webView.estimatedProgress) >= 1.0 {
|
|
|
+ UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
|
|
|
+ self.progressView.alpha = 0
|
|
|
+ }, completion: { _ in
|
|
|
+ self.progressView.setProgress(0.0, animated: false)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
+// if let actionDic = message.body as? NSDictionary {
|
|
|
+// if let action = actionDic["name"] as? String {
|
|
|
+// switch action {
|
|
|
+// case "logout": // MARK: 退出登录
|
|
|
+// logout(isWeb:true)
|
|
|
+// default:
|
|
|
+// Dlog("")
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension JDActivityWebViewController: WKNavigationDelegate {
|
|
|
+ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
+ if let url: URL = navigationAction.request.url {
|
|
|
+ if url.absoluteString.hasPrefix("itms-appss://") || url.absoluteString.hasPrefix("itms-apps://") {
|
|
|
+ if UIApplication.shared.canOpenURL(url) {
|
|
|
+ if #available(iOS 10, *) {
|
|
|
+ UIApplication.shared.open(url, options: [:], completionHandler: {
|
|
|
+ _ in
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ UIApplication.shared.canOpenURL(url)
|
|
|
+ }
|
|
|
+ decisionHandler(.cancel)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ print(url.absoluteString)
|
|
|
+ if UIApplication.shared.canOpenURL(url) && (url.absoluteString == "https://agent.jdnx.com/") {
|
|
|
+ let safariVC = SFSafariViewController(url: url)
|
|
|
+ currentVc()?.navigationController?.present(safariVC, animated: true, completion: nil)
|
|
|
+ decisionHandler(.cancel)
|
|
|
+ return
|
|
|
+ } else if UIApplication.shared.canOpenURL(url) && self.url.length == 0 {
|
|
|
+ let safariVC = SFSafariViewController(url: url)
|
|
|
+ currentVc()?.navigationController?.present(safariVC, animated: true, completion: nil)
|
|
|
+ decisionHandler(.cancel)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ decisionHandler(.allow)
|
|
|
+ }
|
|
|
+
|
|
|
+ func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
|
|
|
+// ProgressHUD.showGIFLoading()
|
|
|
+ }
|
|
|
+
|
|
|
+ func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
+// ProgressHUD.hideHud()
|
|
|
+// ProgressHUD.dismissGIFHud()
|
|
|
+ }
|
|
|
+
|
|
|
+ func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
|
+// ProgressHUD.dismissGIFHud()
|
|
|
+ }
|
|
|
+
|
|
|
+ func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
|
+// ProgressHUD.dismissGIFHud()
|
|
|
+ }
|
|
|
+}
|