Commit 8eafe4a51390dea2b2147457db79acc3c4c2f296
1 parent
dbe52eb3
封装cnWebView
Showing
7 changed files
with
264 additions
and
110 deletions
metaCode.xcodeproj/project.pbxproj
| @@ -92,6 +92,7 @@ | @@ -92,6 +92,7 @@ | ||
| 92 | 1938BD432B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD422B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift */; }; | 92 | 1938BD432B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD422B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift */; }; |
| 93 | 1938BD452B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD442B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift */; }; | 93 | 1938BD452B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD442B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift */; }; |
| 94 | 1938BD472B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD462B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift */; }; | 94 | 1938BD472B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1938BD462B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift */; }; |
| 95 | + 194455A12B6B3310006C5A2A /* CNWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 194455A02B6B3310006C5A2A /* CNWebView.swift */; }; | ||
| 95 | 19565EC02AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EBF2AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift */; }; | 96 | 19565EC02AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EBF2AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift */; }; |
| 96 | 19565EC22AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EC12AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift */; }; | 97 | 19565EC22AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EC12AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift */; }; |
| 97 | 19565EC52AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EC42AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift */; }; | 98 | 19565EC52AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19565EC42AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift */; }; |
| @@ -354,6 +355,7 @@ | @@ -354,6 +355,7 @@ | ||
| 354 | 1938BD422B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNHomepageDuanJuDetailViewController.swift; sourceTree = "<group>"; }; | 355 | 1938BD422B4E34B200390A34 /* CNHomepageDuanJuDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNHomepageDuanJuDetailViewController.swift; sourceTree = "<group>"; }; |
| 355 | 1938BD442B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveDuanjuTitleCell.swift; sourceTree = "<group>"; }; | 356 | 1938BD442B4E49CA00390A34 /* CNLiveDuanjuTitleCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveDuanjuTitleCell.swift; sourceTree = "<group>"; }; |
| 356 | 1938BD462B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveIntegralAlertTool.swift; sourceTree = "<group>"; }; | 357 | 1938BD462B4FD5A500390A34 /* CNLiveIntegralAlertTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveIntegralAlertTool.swift; sourceTree = "<group>"; }; |
| 358 | + 194455A02B6B3310006C5A2A /* CNWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNWebView.swift; sourceTree = "<group>"; }; | ||
| 357 | 19565EBF2AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopDetailBtnsCell.swift; sourceTree = "<group>"; }; | 359 | 19565EBF2AF4DE0E007D6727 /* CNLiveShopDetailBtnsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopDetailBtnsCell.swift; sourceTree = "<group>"; }; |
| 358 | 19565EC12AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopDetailOrderAssetCell.swift; sourceTree = "<group>"; }; | 360 | 19565EC12AF616CA007D6727 /* CNLiveShopDetailOrderAssetCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopDetailOrderAssetCell.swift; sourceTree = "<group>"; }; |
| 359 | 19565EC42AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopProductManagerController.swift; sourceTree = "<group>"; }; | 361 | 19565EC42AF8AD1F007D6727 /* CNLiveShopProductManagerController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CNLiveShopProductManagerController.swift; sourceTree = "<group>"; }; |
| @@ -736,6 +738,7 @@ | @@ -736,6 +738,7 @@ | ||
| 736 | isa = PBXGroup; | 738 | isa = PBXGroup; |
| 737 | children = ( | 739 | children = ( |
| 738 | 19123FC02ABC11170075C883 /* CNWebViewController.swift */, | 740 | 19123FC02ABC11170075C883 /* CNWebViewController.swift */, |
| 741 | + 194455A02B6B3310006C5A2A /* CNWebView.swift */, | ||
| 739 | ); | 742 | ); |
| 740 | path = WebController; | 743 | path = WebController; |
| 741 | sourceTree = "<group>"; | 744 | sourceTree = "<group>"; |
| @@ -1805,6 +1808,7 @@ | @@ -1805,6 +1808,7 @@ | ||
| 1805 | 798A44FF2A30A0A800815935 /* CNLiveInputFeedBackViewController.swift in Sources */, | 1808 | 798A44FF2A30A0A800815935 /* CNLiveInputFeedBackViewController.swift in Sources */, |
| 1806 | 79282A442A2F1C1E00B5F8CF /* CNLiveLoginAuthView.swift in Sources */, | 1809 | 79282A442A2F1C1E00B5F8CF /* CNLiveLoginAuthView.swift in Sources */, |
| 1807 | 79BB96A62AF21E2800AEA4AC /* CNLiveShopOrderDetailViewController.swift in Sources */, | 1810 | 79BB96A62AF21E2800AEA4AC /* CNLiveShopOrderDetailViewController.swift in Sources */, |
| 1811 | + 194455A12B6B3310006C5A2A /* CNWebView.swift in Sources */, | ||
| 1808 | 1938BD2C2B4801A700390A34 /* CNHomePageAnimationTransition.swift in Sources */, | 1812 | 1938BD2C2B4801A700390A34 /* CNHomePageAnimationTransition.swift in Sources */, |
| 1809 | 191733452B284DCD007BCC04 /* CNLiveShopCouponListModel.swift in Sources */, | 1813 | 191733452B284DCD007BCC04 /* CNLiveShopCouponListModel.swift in Sources */, |
| 1810 | 7C2703302A36CBE30061EC50 /* CNCertificationView.swift in Sources */, | 1814 | 7C2703302A36CBE30061EC50 /* CNCertificationView.swift in Sources */, |
metaCode/Classes/Main/HomePage/Controller/AuthMainController/CNMetaCodeHomePageController.swift
| @@ -310,7 +310,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | @@ -310,7 +310,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | ||
| 310 | self.getApplyShopInfo() | 310 | self.getApplyShopInfo() |
| 311 | print("DSShopDarenAuthResultNotificationAction") | 311 | print("DSShopDarenAuthResultNotificationAction") |
| 312 | } | 312 | } |
| 313 | - //刷新当前垂直是哪个页面 | 313 | + // MARK: - 刷新当前垂直是哪个页面 |
| 314 | @objc func kAuthVNumPageChangedNotificationAction(noti:Notification){ | 314 | @objc func kAuthVNumPageChangedNotificationAction(noti:Notification){ |
| 315 | let dict = (noti.userInfo ?? ["":""]) as! [String :Any] | 315 | let dict = (noti.userInfo ?? ["":""]) as! [String :Any] |
| 316 | let num = dict["AuthVNumPage"] as! Int | 316 | let num = dict["AuthVNumPage"] as! Int |
| @@ -431,8 +431,6 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | @@ -431,8 +431,6 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | ||
| 431 | bannerView?.superScrollView = flowView.scrollView | 431 | bannerView?.superScrollView = flowView.scrollView |
| 432 | bannerView?.associatedVCView = self.view | 432 | bannerView?.associatedVCView = self.view |
| 433 | bannerView?.delegate = self | 433 | bannerView?.delegate = self |
| 434 | - bannerView?.upSlideView.webView.navigationDelegate = self | ||
| 435 | - bannerView?.downSlideView.webView.navigationDelegate = self | ||
| 436 | return bannerView | 434 | return bannerView |
| 437 | } | 435 | } |
| 438 | 436 | ||
| @@ -577,64 +575,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | @@ -577,64 +575,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | ||
| 577 | } | 575 | } |
| 578 | } | 576 | } |
| 579 | } | 577 | } |
| 580 | - | ||
| 581 | - // MARK: - WKWebViewDelegate | ||
| 582 | - // 页面开始加载时调用 | ||
| 583 | - func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | ||
| 584 | - print("didStartProvisionalNavigation") | ||
| 585 | - } | ||
| 586 | - // 当内容开始返回时调用 | ||
| 587 | - func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { | ||
| 588 | - print("didCommit") | ||
| 589 | - } | ||
| 590 | - // 页面加载完成之后调用 | ||
| 591 | - func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
| 592 | - print("didFinish") | ||
| 593 | - } | ||
| 594 | - // 页面加载失败时调用 | ||
| 595 | - func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { | ||
| 596 | - print("didFailProvisionalNavigation") | ||
| 597 | - } | ||
| 598 | - // 接收到服务器跳转请求之后调用 | ||
| 599 | - func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { | ||
| 600 | - print("didReceiveServerRedirectForProvisionalNavigation") | ||
| 601 | - } | ||
| 602 | - // 在收到响应后,决定是否跳转 -> 默认允许 | ||
| 603 | - func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { | ||
| 604 | - print("decidePolicyFor") | ||
| 605 | - //允许跳转 | ||
| 606 | - decisionHandler(.allow) | ||
| 607 | - //不允许跳转 | ||
| 608 | -// decisionHandler(.cancel) | ||
| 609 | - } | ||
| 610 | - // 在发送请求之前,决定是否跳转 -> 默认允许 | ||
| 611 | - @available(iOS 13.0, *) | ||
| 612 | - func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) { | ||
| 613 | - print("decidePolicyFor") | ||
| 614 | - let url:String = navigationAction.request.url!.absoluteString | ||
| 615 | - print("navigationAction:\(navigationAction.navigationType.rawValue)") | ||
| 616 | - print(url) | ||
| 617 | - if(navigationAction.navigationType == .linkActivated){ | ||
| 618 | - let webVC = CNWebViewController() | ||
| 619 | - webVC.url = URL(string: url) | ||
| 620 | - navigationViewController().pushViewController(webVC, animated: pushAnimated) | ||
| 621 | -// currentViewController()?.navigationController?.pushViewController(webVC, animated: pushAnimated) | ||
| 622 | - decisionHandler(.cancel, preferences) | ||
| 623 | - return | ||
| 624 | - }else if(navigationAction.navigationType == .other){ | ||
| 625 | - if(url.contains("wjjh5.cnlive.com")){ | ||
| 626 | - | ||
| 627 | - decisionHandler(.allow, preferences) | ||
| 628 | - return | ||
| 629 | - }else | ||
| 630 | - { | ||
| 631 | - decisionHandler(.cancel, preferences) | ||
| 632 | - return | ||
| 633 | - } | ||
| 634 | - } | ||
| 635 | - decisionHandler(.allow, preferences) | ||
| 636 | - } | ||
| 637 | - | 578 | + |
| 638 | //MARK: - 网络请求获取身份列表 | 579 | //MARK: - 网络请求获取身份列表 |
| 639 | func getUserIdentityApi() | 580 | func getUserIdentityApi() |
| 640 | { | 581 | { |
metaCode/Classes/Main/HomePage/Controller/WebController/CNWebView.swift
0 → 100644
| 1 | +// | ||
| 2 | +// CNWebView.swift | ||
| 3 | +// metaCode | ||
| 4 | +// | ||
| 5 | +// Created by zx on 2024/2/1. | ||
| 6 | +// | ||
| 7 | + | ||
| 8 | +import Foundation | ||
| 9 | +import WebKit | ||
| 10 | +import WebViewJavascriptBridge | ||
| 11 | +import CNLiveShareToolKit | ||
| 12 | +import CommonCrypto | ||
| 13 | +import CNLiveBusinessTools | ||
| 14 | + | ||
| 15 | +class CNWebView:UIView,WKNavigationDelegate{ | ||
| 16 | + | ||
| 17 | + var configuration:WKWebViewConfiguration? | ||
| 18 | + | ||
| 19 | + lazy var cnWebView :WKWebView = { | ||
| 20 | + let cnWebView = WKWebView(frame: self.bounds, configuration: self.configuration!) | ||
| 21 | + return cnWebView | ||
| 22 | + }() | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + var webUrl:URL?//初始URL | ||
| 26 | + var currentUrl:String = ""//当前网页的url | ||
| 27 | + var bridge:WKWebViewJavascriptBridge? | ||
| 28 | + | ||
| 29 | + init(frame: CGRect, configuration: WKWebViewConfiguration ,url:URL?) { | ||
| 30 | + super.init(frame: frame) | ||
| 31 | + self.configuration = configuration | ||
| 32 | + self.addSubview(self.cnWebView) | ||
| 33 | + self.webUrl = url | ||
| 34 | + self.cnWebView.evaluateJavaScript("navigator.userAgent") { [self] result, error in | ||
| 35 | + let userAgent = result as! String | ||
| 36 | + if error == nil && userAgent.count > 0 { | ||
| 37 | + if !userAgent.contains(CNLiveMCEnvironmentManager.shared.base_web_UA) { | ||
| 38 | + let newUserAgent = userAgent + CNLiveMCEnvironmentManager.shared.base_web_UA | ||
| 39 | + self.cnWebView.customUserAgent = newUserAgent | ||
| 40 | + | ||
| 41 | + if (self.webUrl != nil){ | ||
| 42 | + let urlStr = self.checkURLWithStr(urlStr: self.webUrl!.absoluteString) | ||
| 43 | + self.currentUrl = urlStr | ||
| 44 | + | ||
| 45 | + self.cnWebView.load(URLRequest.init(url:URL(string: urlStr)!)) | ||
| 46 | + } | ||
| 47 | + self.configBridge() | ||
| 48 | + self.configBridgeRegister() | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + func configBridge(){ | ||
| 55 | + WKWebViewJavascriptBridge.enableLogging() | ||
| 56 | + self.bridge = WKWebViewJavascriptBridge(for: self.cnWebView) | ||
| 57 | + self.bridge?.setWebViewDelegate(self) | ||
| 58 | + } | ||
| 59 | + //h5拉起原生分享 js call oc | ||
| 60 | + func configBridgeRegister() -> Void { | ||
| 61 | + self.bridge?.registerHandler("wjjylzgVideoShare", handler: { data, responseCallback in | ||
| 62 | +// print("js call oc\(data)") | ||
| 63 | + let dic = data as! Dictionary<String, String> | ||
| 64 | + CNLiveShareManager.showShareViewWithParam(forShareTitle: dic["title"], shareUrl: dic["shareUrl"], shareDesc: dic["subTitle"], shareImage: dic["cover"], screenFull: false, hiddenWjj: true, hiddenQQ: false, hiddenWB: true, hiddenLifeCircle: true, hiddenWechatCircle: false, hiddenWechat: false, hiddenSafari: true, formVC: currentViewController(), topImage: [], topTitles: [], platformType: .all) { title in | ||
| 65 | + | ||
| 66 | + } completerBlock: { resultType, platformType, typeString in | ||
| 67 | + if resultType == .succ{ | ||
| 68 | + | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + }) | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + required init?(coder: NSCoder) { | ||
| 76 | + fatalError("init(coder:) has not been implemented") | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + func checkURLWithStr(urlStr:String) -> String{ | ||
| 80 | + if URL(string: urlStr)?.host != WjjH5 { | ||
| 81 | + return urlStr//不是wjjh5不作处理 | ||
| 82 | + } | ||
| 83 | + var str = urlStr | ||
| 84 | + let date = Date() | ||
| 85 | + let formatter = DateFormatter() | ||
| 86 | + formatter.dateFormat = "yyyyMMdd" | ||
| 87 | + let dateStr = formatter.string(from: date) | ||
| 88 | + let sidStr = String(format: "%@%@", UserInfoManager.shared.userInfo.uid,dateStr) | ||
| 89 | + let signStr = sidStr.crypt(operation: CCOperation(kCCEncrypt), key: CNLiveMCEnvironmentManager.shared.appKey) | ||
| 90 | + //看是 appKey还是appKeywjj | ||
| 91 | + let sidSignStr = (signStr ?? "").isEmpty ? "" : signStr | ||
| 92 | + let sourceArray = str.components(separatedBy: "?") | ||
| 93 | + let buildVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String | ||
| 94 | + let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String | ||
| 95 | + let uuid = CNLiveBusinessTools.getUidForStat(Date()) | ||
| 96 | + if sourceArray.count == 1{ | ||
| 97 | + //是wjjh5且无参数 | ||
| 98 | + str = String(format: "%@?shareSid=%@&sid=%@&plat=i&ver=%@&version=%@&wjjFrom=apple&wjjUUID=%@&appId=%@&voteId=%@&wjjToken=%@", str,UserInfoManager.shared.userInfo.uid,UserInfoManager.shared.userInfo.uid,buildVersion,appVersion,uuid,CNLiveMCEnvironmentManager.shared.appId,sidSignStr!,UserInfoManager.shared.userInfo.token) | ||
| 99 | + return str | ||
| 100 | + } | ||
| 101 | + //是wjjh5且有参数 | ||
| 102 | + let paraArr = NSMutableArray(array: ["shareSid=","sid=","plat=","ver=","version=","wjjFrom=","wjjUUID=","appId=","voteId=","wjjToken="]) | ||
| 103 | + let valueArr = NSMutableArray(array:[UserInfoManager.shared.userInfo.uid,UserInfoManager.shared.userInfo.uid,"i",buildVersion,appVersion,"apple",uuid,CNLiveMCEnvironmentManager.shared.appId,sidSignStr!,UserInfoManager.shared.userInfo.token]) | ||
| 104 | + var i = 0 | ||
| 105 | + for paraStr in paraArr{ | ||
| 106 | + let tempStr = paraStr as! String | ||
| 107 | + let paramStr = "&".appending(tempStr) | ||
| 108 | + let paramStr2 = "?".appending(tempStr) | ||
| 109 | + | ||
| 110 | + if str.contains(paramStr){ | ||
| 111 | + //包含则替换 &sid= | ||
| 112 | + let lastStr = str.components(separatedBy: tempStr).last | ||
| 113 | + let lastSeparatedStr = (lastStr?.components(separatedBy: "&").first)! | ||
| 114 | + let oldStr = tempStr.appending(lastSeparatedStr) | ||
| 115 | + let tempValueStr = tempStr.appending(valueArr[i] as! String) | ||
| 116 | + str = str.replacingOccurrences(of: oldStr, with: tempValueStr) | ||
| 117 | + }else if str.contains(paramStr2){ | ||
| 118 | + //包含则替换 &sid= | ||
| 119 | + let lastStr = str.components(separatedBy: tempStr).last | ||
| 120 | + let lastSeparatedStr = (lastStr?.components(separatedBy: "&").first)! | ||
| 121 | + let oldStr = tempStr.appending(lastSeparatedStr) | ||
| 122 | + let tempValueStr = tempStr.appending(valueArr[i] as! String) | ||
| 123 | + str = str.replacingOccurrences(of: oldStr, with: tempValueStr) | ||
| 124 | + }else{ | ||
| 125 | + //不包含则拼接 | ||
| 126 | + str = String(format: "%@&%@%@", str,tempStr,valueArr[i] as! String) | ||
| 127 | + } | ||
| 128 | + i = i + 1 | ||
| 129 | + } | ||
| 130 | + return str | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + // MARK: - WKWebViewDelegate | ||
| 134 | + // 页面开始加载时调用 | ||
| 135 | + func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | ||
| 136 | + print("didStartProvisionalNavigation") | ||
| 137 | + } | ||
| 138 | + // 当内容开始返回时调用 | ||
| 139 | + func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { | ||
| 140 | + print("didCommit") | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + // 页面加载完成之后调用 | ||
| 144 | + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
| 145 | + print("didFinish") | ||
| 146 | +// titleName = webView.title! | ||
| 147 | + } | ||
| 148 | + // 页面加载失败时调用 | ||
| 149 | + func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { | ||
| 150 | + print("didFailProvisionalNavigation") | ||
| 151 | + } | ||
| 152 | + // 接收到服务器跳转请求之后调用 | ||
| 153 | + func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { | ||
| 154 | + print("didReceiveServerRedirectForProvisionalNavigation") | ||
| 155 | + } | ||
| 156 | + // 在收到响应后,决定是否跳转 -> 默认允许 | ||
| 157 | + func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { | ||
| 158 | + | ||
| 159 | + print("decidePolicyFornavigationResponse") | ||
| 160 | + | ||
| 161 | + //允许跳转 | ||
| 162 | + decisionHandler(.allow) | ||
| 163 | + //不允许跳转 | ||
| 164 | + // decisionHandler(.cancel) | ||
| 165 | + } | ||
| 166 | + // 在发送请求之前,决定是否跳转 -> 默认允许 | ||
| 167 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { | ||
| 168 | + print("decidePolicyFornavigationAction") | ||
| 169 | + | ||
| 170 | + var urlStr = (navigationAction.request.url?.absoluteString)! | ||
| 171 | + | ||
| 172 | + if navigationAction.request.url?.host == WjjH5 { | ||
| 173 | + | ||
| 174 | +// if urlStr.contains("&isWjjReport=true") || urlStr.contains("?isWjjReport=true"){ | ||
| 175 | +// self.rightNavBtns = [self.moreNavBtn] | ||
| 176 | +// self.moreNavBtn.isHidden = false | ||
| 177 | +// }else{ | ||
| 178 | +// self.moreNavBtn.isHidden = true | ||
| 179 | +// } | ||
| 180 | + | ||
| 181 | + urlStr = self.checkURLWithStr(urlStr: urlStr) | ||
| 182 | + self.currentUrl = urlStr | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + if navigationAction.navigationType == .other{ | ||
| 186 | + //跳转Appstore | ||
| 187 | + 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") | ||
| 188 | + if url == navigationAction.request.url { | ||
| 189 | + if UIApplication.shared.canOpenURL(url!) { UIApplication.shared.open(url!, options: [:], completionHandler: nil) | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | + }else if navigationAction.navigationType == .linkActivated{ | ||
| 193 | + let webVC = CNWebViewController() | ||
| 194 | + webVC.url = URL(string: urlStr) | ||
| 195 | + navigationViewController().pushViewController(webVC, animated: true) | ||
| 196 | + decisionHandler(.cancel) | ||
| 197 | + return | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + decisionHandler(.allow) | ||
| 201 | + } | ||
| 202 | +} |
metaCode/Classes/Main/HomePage/Controller/WebController/CNWebViewController.swift
| @@ -14,6 +14,7 @@ import CNLiveBusinessTools | @@ -14,6 +14,7 @@ import CNLiveBusinessTools | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | let WjjH5 = "wjjh5.cnlive.com" | 16 | let WjjH5 = "wjjh5.cnlive.com" |
| 17 | +let ManageshopH5 = "manageshop.cnlive.com" | ||
| 17 | 18 | ||
| 18 | class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ | 19 | class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 19 | 20 | ||
| @@ -265,7 +266,14 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ | @@ -265,7 +266,14 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ | ||
| 265 | } | 266 | } |
| 266 | } | 267 | } |
| 267 | }else if navigationAction.navigationType == .linkActivated{ | 268 | }else if navigationAction.navigationType == .linkActivated{ |
| 268 | - | 269 | + |
| 270 | + //以后可判断h5地址 ,可跳转原生页面或网页 | ||
| 271 | + //暂时只跳h5 | ||
| 272 | + let webVC = CNWebViewController() | ||
| 273 | + webVC.url = URL(string: urlStr) | ||
| 274 | + navigationViewController().pushViewController(webVC, animated: pushAnimated) | ||
| 275 | + decisionHandler(.cancel) | ||
| 276 | + return | ||
| 269 | 277 | ||
| 270 | } | 278 | } |
| 271 | decisionHandler(.allow) | 279 | decisionHandler(.allow) |
metaCode/Classes/Main/HomePage/View/VerticalViews/CNCustomIndexView.swift
| @@ -121,8 +121,8 @@ class CNCustomIndexView:PGIndexBannerSubiew, UIScrollViewDelegate, WJColorChange | @@ -121,8 +121,8 @@ class CNCustomIndexView:PGIndexBannerSubiew, UIScrollViewDelegate, WJColorChange | ||
| 121 | self.upSlideView.type = _idModel?.downType | 121 | self.upSlideView.type = _idModel?.downType |
| 122 | self.upSlideView.downPath = _idModel?.downPath | 122 | self.upSlideView.downPath = _idModel?.downPath |
| 123 | 123 | ||
| 124 | - self.downSlideView.type = _idModel?.upType | ||
| 125 | - self.downSlideView.downPath = _idModel?.upPath | 124 | + self.downSlideView.type = _idModel?.upType |
| 125 | + self.downSlideView.downPath = _idModel?.upPath | ||
| 126 | 126 | ||
| 127 | self.topNavView.setTitleString(title:(_idModel?.name)!) | 127 | self.topNavView.setTitleString(title:(_idModel?.name)!) |
| 128 | 128 |
metaCode/Classes/Main/HomePage/View/VerticalViews/DownSlide/CNDownSlideView.swift
| @@ -23,28 +23,30 @@ class CNDownSlideView:UIView{ | @@ -23,28 +23,30 @@ class CNDownSlideView:UIView{ | ||
| 23 | let contentView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.width, height: self.height)) | 23 | let contentView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.width, height: self.height)) |
| 24 | contentView.backgroundColor = UIColor.clear | 24 | contentView.backgroundColor = UIColor.clear |
| 25 | contentView.isUserInteractionEnabled = true | 25 | contentView.isUserInteractionEnabled = true |
| 26 | -// let path:UIBezierPath = UIBezierPath.init(roundedRect: contentView.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize.init(width: 20, height: 20)) | ||
| 27 | -// | ||
| 28 | -// let maskLayer:CAShapeLayer = CAShapeLayer() | ||
| 29 | -// maskLayer.frame = contentView.bounds | ||
| 30 | -// maskLayer.path = path.cgPath | ||
| 31 | -// contentView.layer.mask = maskLayer | ||
| 32 | - | ||
| 33 | -// let imageView = UIImageView(frame: CGRect(x: 10, y: 20, width: contentView.width - 20, height: contentView.height - 40)) | ||
| 34 | -// imageView.image = UIImage(named: "homePage_collectMoneyImage") | ||
| 35 | -// contentView.addSubview(imageView) | ||
| 36 | - | ||
| 37 | return contentView | 26 | return contentView |
| 38 | }() | 27 | }() |
| 39 | 28 | ||
| 40 | //h5网页 | 29 | //h5网页 |
| 41 | - // TODO: H:-10原为self.height-KTabBarHeight,但有白条,改成了-10 | ||
| 42 | - lazy var webView: WKWebView = { | ||
| 43 | - let webView = WKWebView.init(frame: CGRectMake(0, 0, self.width, self.height-KTabBarHeight-10)) | ||
| 44 | - webView.scrollView.showsVerticalScrollIndicator = false | ||
| 45 | - webView.scrollView.showsHorizontalScrollIndicator = false | ||
| 46 | - webView.backgroundColor = .clear | ||
| 47 | - webView.isOpaque = false | 30 | + lazy var webView: CNWebView = { |
| 31 | + let configuration = WKWebViewConfiguration() | ||
| 32 | + configuration.allowsInlineMediaPlayback = true | ||
| 33 | + let preferences = WKPreferences() | ||
| 34 | + preferences.javaScriptCanOpenWindowsAutomatically = true | ||
| 35 | + preferences.javaScriptEnabled = true | ||
| 36 | + configuration.preferences.minimumFontSize = 0.0 | ||
| 37 | + configuration.preferences = preferences | ||
| 38 | + configuration.userContentController = WKUserContentController() | ||
| 39 | + | ||
| 40 | + var url:URL? | ||
| 41 | + if (_downPath != ""){ | ||
| 42 | + url = URL.init(string: _downPath!) | ||
| 43 | + }else{ | ||
| 44 | + url = nil | ||
| 45 | + } | ||
| 46 | + let webView = CNWebView(frame: CGRectMake(0, 0, Width, self.height-DownSlideBottomViewHeight), configuration: configuration,url: url) | ||
| 47 | + | ||
| 48 | + webView.cnWebView.scrollView.showsVerticalScrollIndicator = false | ||
| 49 | + webView.cnWebView.scrollView.showsHorizontalScrollIndicator = false | ||
| 48 | return webView | 50 | return webView |
| 49 | }() | 51 | }() |
| 50 | 52 | ||
| @@ -74,12 +76,9 @@ class CNDownSlideView:UIView{ | @@ -74,12 +76,9 @@ class CNDownSlideView:UIView{ | ||
| 74 | // self.subviews.forEach { view in | 76 | // self.subviews.forEach { view in |
| 75 | // view.removeFromSuperview() | 77 | // view.removeFromSuperview() |
| 76 | // }//会删除button | 78 | // }//会删除button |
| 79 | + | ||
| 77 | if(type == UpDownTypeH5){ | 80 | if(type == UpDownTypeH5){ |
| 78 | self.addSubview(self.webView) | 81 | self.addSubview(self.webView) |
| 79 | - if (_downPath != ""){ | ||
| 80 | - let webUrl: URL! = URL.init(string: _downPath!) | ||
| 81 | - webView.load(URLRequest.init(url:webUrl)) | ||
| 82 | - } | ||
| 83 | }else if(type == UpDownTypePage){ | 82 | }else if(type == UpDownTypePage){ |
| 84 | self.addSubview(self.upSlideVC.view) | 83 | self.addSubview(self.upSlideVC.view) |
| 85 | self.upSlideVC.downPath = _downPath | 84 | self.upSlideVC.downPath = _downPath |
| @@ -108,18 +107,6 @@ class CNDownSlideView:UIView{ | @@ -108,18 +107,6 @@ class CNDownSlideView:UIView{ | ||
| 108 | let bottomView = UIView(frame: CGRect(x:0, y:self.height - DownSlideBottomViewHeight, width: self.width, height: DownSlideBottomViewHeight)) | 107 | let bottomView = UIView(frame: CGRect(x:0, y:self.height - DownSlideBottomViewHeight, width: self.width, height: DownSlideBottomViewHeight)) |
| 109 | bottomView.backgroundColor = HexColor("#FFFFFF") | 108 | bottomView.backgroundColor = HexColor("#FFFFFF") |
| 110 | bottomView.isHidden = true | 109 | bottomView.isHidden = true |
| 111 | - | ||
| 112 | -// let path:UIBezierPath = UIBezierPath.init(roundedRect: bottomView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize.init(width: 50, height: 50)) | ||
| 113 | -// let maskLayer:CAShapeLayer = CAShapeLayer() | ||
| 114 | -// maskLayer.frame = bottomView.bounds | ||
| 115 | -// maskLayer.path = path.cgPath | ||
| 116 | -// bottomView.layer.mask = maskLayer | ||
| 117 | -// bottomView.clipsToBounds = true | ||
| 118 | - | ||
| 119 | -// bottomView.layer.cornerRadius = 50 | ||
| 120 | -// bottomView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] | ||
| 121 | - | ||
| 122 | - | ||
| 123 | // 设置阴影效果 | 110 | // 设置阴影效果 |
| 124 | bottomView.layer.masksToBounds = false | 111 | bottomView.layer.masksToBounds = false |
| 125 | //定义view的阴影颜色 | 112 | //定义view的阴影颜色 |
metaCode/Classes/Main/HomePage/View/VerticalViews/UpSlide/CNUpSlideView.swift
| @@ -18,14 +18,30 @@ let UpDownTypeAuthAlertDown = 6///下滑地址 认证-弹窗 | @@ -18,14 +18,30 @@ let UpDownTypeAuthAlertDown = 6///下滑地址 认证-弹窗 | ||
| 18 | ///上滑显示的VIew | 18 | ///上滑显示的VIew |
| 19 | class CNUpSlideView:UIView{ | 19 | class CNUpSlideView:UIView{ |
| 20 | 20 | ||
| 21 | - lazy var webView: WKWebView = { | ||
| 22 | - let webView = WKWebView.init(frame: CGRectMake(10, KNavigationHeight+KStatusBarHeight+10, self.width - 20, self.height-KNavigationHeight-KStatusBarHeight-10)) | ||
| 23 | - webView.scrollView.showsVerticalScrollIndicator = false | ||
| 24 | - webView.scrollView.showsHorizontalScrollIndicator = false | ||
| 25 | - webView.backgroundColor = .clear | ||
| 26 | - webView.layer.cornerRadius = 29 | ||
| 27 | - webView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] | ||
| 28 | - webView.clipsToBounds = true | 21 | + lazy var webView: CNWebView = { |
| 22 | + let configuration = WKWebViewConfiguration() | ||
| 23 | + configuration.allowsInlineMediaPlayback = true | ||
| 24 | + let preferences = WKPreferences() | ||
| 25 | + preferences.javaScriptCanOpenWindowsAutomatically = true | ||
| 26 | + preferences.javaScriptEnabled = true | ||
| 27 | + configuration.preferences.minimumFontSize = 0.0 | ||
| 28 | + configuration.preferences = preferences | ||
| 29 | + configuration.userContentController = WKUserContentController() | ||
| 30 | + | ||
| 31 | + var url:URL? | ||
| 32 | + if (_downPath != ""){ | ||
| 33 | + url = URL.init(string: _downPath!) | ||
| 34 | + }else{ | ||
| 35 | + url = nil | ||
| 36 | + } | ||
| 37 | + let webView = CNWebView(frame: CGRectMake(10, KNavigationHeight+KStatusBarHeight+10, self.width - 20, self.height-KNavigationHeight-KStatusBarHeight-10), configuration: configuration,url: url) | ||
| 38 | + | ||
| 39 | + webView.cnWebView.scrollView.showsVerticalScrollIndicator = false | ||
| 40 | + webView.cnWebView.scrollView.showsHorizontalScrollIndicator = false | ||
| 41 | + webView.cnWebView.backgroundColor = .clear | ||
| 42 | + webView.cnWebView.layer.cornerRadius = 29 | ||
| 43 | + webView.cnWebView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] | ||
| 44 | + webView.cnWebView.clipsToBounds = true | ||
| 29 | return webView | 45 | return webView |
| 30 | }() | 46 | }() |
| 31 | 47 | ||
| @@ -51,10 +67,6 @@ class CNUpSlideView:UIView{ | @@ -51,10 +67,6 @@ class CNUpSlideView:UIView{ | ||
| 51 | } | 67 | } |
| 52 | if(type == UpDownTypeH5){ | 68 | if(type == UpDownTypeH5){ |
| 53 | self.addSubview(self.webView) | 69 | self.addSubview(self.webView) |
| 54 | - if (_downPath != ""){ | ||
| 55 | - let webUrl: URL! = URL.init(string: _downPath!) | ||
| 56 | - webView.load(URLRequest.init(url:webUrl)) | ||
| 57 | - } | ||
| 58 | } | 70 | } |
| 59 | else if(type == UpDownTypePage){ | 71 | else if(type == UpDownTypePage){ |
| 60 | 72 |