Commit bc4912d58688d86cbaae9dac65a9c4f6b7855260
1 parent
b5e11e8f
webView交互 , 分享...
Showing
2 changed files
with
139 additions
and
18 deletions
metaCode/Classes/Extensions/String+Extension.swift
| ... | ... | @@ -7,7 +7,7 @@ |
| 7 | 7 | |
| 8 | 8 | |
| 9 | 9 | import UIKit |
| 10 | - | |
| 10 | +import CommonCrypto | |
| 11 | 11 | |
| 12 | 12 | extension String { |
| 13 | 13 | /// Calculate text's height from `width` and `front`. |
| ... | ... | @@ -25,5 +25,63 @@ extension String { |
| 25 | 25 | let substring = self[startIndex..<endIndex] |
| 26 | 26 | return String(substring) |
| 27 | 27 | } |
| 28 | + | |
| 29 | + //加密 | |
| 30 | + func crypt(operation:CCOperation, key: String) -> String? { | |
| 31 | + | |
| 32 | + if let keyData = key.data(using: .utf8) { | |
| 33 | + | |
| 34 | + var cryptData: Data? | |
| 35 | + | |
| 36 | + if operation == kCCEncrypt { | |
| 37 | + cryptData = self.data(using: .utf8) | |
| 38 | + } else { | |
| 39 | + cryptData = Data(base64Encoded: self, options: .ignoreUnknownCharacters) | |
| 40 | + } | |
| 41 | + | |
| 42 | + if cryptData == nil { | |
| 43 | + return nil | |
| 44 | + } | |
| 45 | + | |
| 46 | + let algoritm: CCAlgorithm = CCAlgorithm(kCCAlgorithmDES) | |
| 47 | + let option: CCOptions = CCOptions(kCCOptionPKCS7Padding) | |
| 48 | + | |
| 49 | + let keyBytes = [UInt8](keyData) | |
| 50 | + let keyLength = kCCKeySizeDES | |
| 51 | + | |
| 52 | + let dataIn = [UInt8](cryptData!) | |
| 53 | + let dataInlength = cryptData!.count | |
| 54 | + | |
| 55 | + let dataOutAvailable = Int(dataInlength + kCCBlockSizeDES) | |
| 56 | + let dataOut = UnsafeMutablePointer<UInt8>.allocate(capacity: dataOutAvailable) | |
| 57 | + let dataOutMoved = UnsafeMutablePointer<Int>.allocate(capacity: 1) | |
| 58 | + | |
| 59 | + dataOutMoved.initialize(to: 0) | |
| 60 | + | |
| 61 | + let cryptStatus = CCCrypt(operation, algoritm, option, keyBytes, keyLength, keyBytes, dataIn, dataInlength, dataOut, dataOutAvailable, dataOutMoved) | |
| 62 | + | |
| 63 | + var data: Data? | |
| 64 | + | |
| 65 | + if CCStatus(cryptStatus) == CCStatus(kCCSuccess) { | |
| 66 | + data = Data(bytesNoCopy: dataOut, count: dataOutMoved.pointee, deallocator: .none) | |
| 67 | + } | |
| 68 | + | |
| 69 | + dataOutMoved.deallocate() | |
| 70 | + dataOut.deallocate() | |
| 71 | + | |
| 72 | + if data == nil { | |
| 73 | + return nil | |
| 74 | + } | |
| 75 | + | |
| 76 | + if operation == kCCEncrypt { | |
| 77 | + data = data!.base64EncodedData(options: .lineLength64Characters) | |
| 78 | + } | |
| 79 | + | |
| 80 | + return String(data: data!, encoding: .utf8) | |
| 81 | + } | |
| 82 | + | |
| 83 | + return nil | |
| 84 | + | |
| 85 | + } | |
| 28 | 86 | |
| 29 | 87 | } | ... | ... |
metaCode/Classes/Main/HomePage/Controller/WebController/CNWebViewController.swift
| ... | ... | @@ -9,6 +9,11 @@ import Foundation |
| 9 | 9 | import WebKit |
| 10 | 10 | import WebViewJavascriptBridge |
| 11 | 11 | import CNLiveShareToolKit |
| 12 | +import CommonCrypto | |
| 13 | +import CNLiveBusinessTools | |
| 14 | + | |
| 15 | + | |
| 16 | +let WjjH5 = "wjjh5.cnlive.com" | |
| 12 | 17 | |
| 13 | 18 | class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 14 | 19 | |
| ... | ... | @@ -63,13 +68,15 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 63 | 68 | webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in |
| 64 | 69 | let userAgent = result as! String |
| 65 | 70 | if error == nil && userAgent.count > 0 { |
| 66 | - if !userAgent.contains(CNLiveEnvironmentManager.shared.base_web_UA) { | |
| 67 | - let newUserAgent = userAgent + CNLiveEnvironmentManager.shared.base_web_UA | |
| 71 | + if !userAgent.contains(CNLiveMCEnvironmentManager.shared.base_web_UA) { | |
| 72 | + let newUserAgent = userAgent + CNLiveMCEnvironmentManager.shared.base_web_UA | |
| 68 | 73 | webView.customUserAgent = newUserAgent |
| 69 | 74 | |
| 70 | 75 | if (_url != nil){ |
| 71 | - let webUrl: URL! = _url! | |
| 72 | - webView.load(URLRequest.init(url:webUrl)) | |
| 76 | + let urlStr = self.checkURLWithStr(urlStr: _url!.absoluteString) | |
| 77 | + self.currentUrl = urlStr | |
| 78 | + | |
| 79 | + webView.load(URLRequest.init(url:URL(string: urlStr)!)) | |
| 73 | 80 | } |
| 74 | 81 | self.configBridge() |
| 75 | 82 | self.configBridgeRegister() |
| ... | ... | @@ -102,7 +109,7 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 102 | 109 | //点击...分享获取h5参数oc call js |
| 103 | 110 | func configBridgeCallHander() -> Void { |
| 104 | 111 | self.bridge?.callHandler("wjjClientGetShareParaFromJS", data: ["":""], responseCallback: { data in |
| 105 | - print("js call oc\(data)") | |
| 112 | +// print("js call oc\(data)") | |
| 106 | 113 | let dic = data as! Dictionary<String, String> |
| 107 | 114 | // let shareType = dic["shareType"] |
| 108 | 115 | // let contentIdStr = dic["activityId"] |
| ... | ... | @@ -111,7 +118,7 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 111 | 118 | let shareImg = dic["shareImg"] |
| 112 | 119 | |
| 113 | 120 | var shareUrl = "" |
| 114 | - if var tempShareUrl = dic["shareUrl"]{ | |
| 121 | + if let tempShareUrl = dic["shareUrl"]{ | |
| 115 | 122 | if tempShareUrl == ""{ |
| 116 | 123 | shareUrl = self.currentUrl |
| 117 | 124 | }else{ |
| ... | ... | @@ -130,6 +137,61 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 130 | 137 | } |
| 131 | 138 | }) |
| 132 | 139 | } |
| 140 | + | |
| 141 | + func checkURLWithStr(urlStr:String) -> String{ | |
| 142 | + if URL(string: urlStr)?.host != WjjH5 { | |
| 143 | + return urlStr//不是wjjh5不作处理 | |
| 144 | + } | |
| 145 | + var str = urlStr | |
| 146 | + let date = Date() | |
| 147 | + let formatter = DateFormatter() | |
| 148 | + formatter.dateFormat = "yyyyMMdd" | |
| 149 | + let dateStr = formatter.string(from: date) | |
| 150 | + let sidStr = String(format: "%@%@", UserInfoManager.shared.userInfo.uid,dateStr) | |
| 151 | + let signStr = sidStr.crypt(operation: CCOperation(kCCEncrypt), key: CNLiveMCEnvironmentManager.shared.appKey) | |
| 152 | + //看是 appKey还是appKeywjj | |
| 153 | + let sidSignStr = (signStr ?? "").isEmpty ? "" : signStr | |
| 154 | + let sourceArray = str.components(separatedBy: "?") | |
| 155 | + let buildVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String | |
| 156 | + let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String | |
| 157 | + let uuid = CNLiveBusinessTools.getUidForStat(Date()) | |
| 158 | + if sourceArray.count == 1{ | |
| 159 | + //是wjjh5且无参数 | |
| 160 | + 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) | |
| 161 | + return str | |
| 162 | + } | |
| 163 | + //是wjjh5且有参数 | |
| 164 | + let paraArr = NSMutableArray(array: ["shareSid=","sid=","plat=","ver=","version=","wjjFrom=","wjjUUID=","appId=","voteId=","wjjToken="]) | |
| 165 | + 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]) | |
| 166 | + var i = 0 | |
| 167 | + for paraStr in paraArr{ | |
| 168 | + let tempStr = paraStr as! String | |
| 169 | + let paramStr = "&".appending(tempStr) | |
| 170 | + let paramStr2 = "?".appending(tempStr) | |
| 171 | + | |
| 172 | + if str.contains(paramStr){ | |
| 173 | + //包含则替换 &sid= | |
| 174 | + let lastStr = str.components(separatedBy: tempStr).last | |
| 175 | + let lastSeparatedStr = (lastStr?.components(separatedBy: "&").first)! | |
| 176 | + let oldStr = tempStr.appending(lastSeparatedStr) | |
| 177 | + let tempValueStr = tempStr.appending(valueArr[i] as! String) | |
| 178 | + str = str.replacingOccurrences(of: oldStr, with: tempValueStr) | |
| 179 | + }else if str.contains(paramStr2){ | |
| 180 | + //包含则替换 &sid= | |
| 181 | + let lastStr = str.components(separatedBy: tempStr).last | |
| 182 | + let lastSeparatedStr = (lastStr?.components(separatedBy: "&").first)! | |
| 183 | + let oldStr = tempStr.appending(lastSeparatedStr) | |
| 184 | + let tempValueStr = tempStr.appending(valueArr[i] as! String) | |
| 185 | + str = str.replacingOccurrences(of: oldStr, with: tempValueStr) | |
| 186 | + }else{ | |
| 187 | + //不包含则拼接 | |
| 188 | + str = String(format: "%@&%@%@", str,tempStr,valueArr[i] as! String) | |
| 189 | + } | |
| 190 | + i = i + 1 | |
| 191 | + } | |
| 192 | + return str | |
| 193 | + } | |
| 194 | + | |
| 133 | 195 | // MARK: - BtnAction |
| 134 | 196 | // 内部链接右上角···分享 |
| 135 | 197 | @objc func moreNavBtnAction(){ |
| ... | ... | @@ -146,30 +208,30 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 146 | 208 | // MARK: - WKWebViewDelegate |
| 147 | 209 | // 页面开始加载时调用 |
| 148 | 210 | func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { |
| 149 | - print("didStartProvisionalNavigation") | |
| 211 | +// print("didStartProvisionalNavigation") | |
| 150 | 212 | } |
| 151 | 213 | // 当内容开始返回时调用 |
| 152 | 214 | func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { |
| 153 | - print("didCommit") | |
| 215 | +// print("didCommit") | |
| 154 | 216 | } |
| 155 | 217 | |
| 156 | 218 | // 页面加载完成之后调用 |
| 157 | 219 | func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 158 | - print("didFinish") | |
| 220 | +// print("didFinish") | |
| 159 | 221 | titleName = webView.title! |
| 160 | 222 | } |
| 161 | 223 | // 页面加载失败时调用 |
| 162 | 224 | func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { |
| 163 | - print("didFailProvisionalNavigation") | |
| 225 | +// print("didFailProvisionalNavigation") | |
| 164 | 226 | } |
| 165 | 227 | // 接收到服务器跳转请求之后调用 |
| 166 | 228 | func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { |
| 167 | - print("didReceiveServerRedirectForProvisionalNavigation") | |
| 229 | +// print("didReceiveServerRedirectForProvisionalNavigation") | |
| 168 | 230 | } |
| 169 | 231 | // 在收到响应后,决定是否跳转 -> 默认允许 |
| 170 | 232 | func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { |
| 171 | 233 | |
| 172 | - print("decidePolicyFornavigationResponse") | |
| 234 | +// print("decidePolicyFornavigationResponse") | |
| 173 | 235 | |
| 174 | 236 | //允许跳转 |
| 175 | 237 | decisionHandler(.allow) |
| ... | ... | @@ -178,12 +240,11 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 178 | 240 | } |
| 179 | 241 | // 在发送请求之前,决定是否跳转 -> 默认允许 |
| 180 | 242 | func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
| 181 | - print("decidePolicyFornavigationAction") | |
| 243 | +// print("decidePolicyFornavigationAction") | |
| 182 | 244 | |
| 183 | - let urlStr = (navigationAction.request.url?.absoluteString)! | |
| 245 | + var urlStr = (navigationAction.request.url?.absoluteString)! | |
| 184 | 246 | |
| 185 | - if navigationAction.request.url?.host == "wjjh5.cnlive.com"{ | |
| 186 | - self.currentUrl = urlStr | |
| 247 | + if navigationAction.request.url?.host == WjjH5 { | |
| 187 | 248 | |
| 188 | 249 | if urlStr.contains("&isWjjReport=true") || urlStr.contains("?isWjjReport=true"){ |
| 189 | 250 | self.rightNavBtns = [self.moreNavBtn] |
| ... | ... | @@ -191,8 +252,10 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 191 | 252 | }else{ |
| 192 | 253 | self.moreNavBtn.isHidden = true |
| 193 | 254 | } |
| 255 | + | |
| 256 | + urlStr = self.checkURLWithStr(urlStr: urlStr) | |
| 257 | + self.currentUrl = urlStr | |
| 194 | 258 | } |
| 195 | - | |
| 196 | 259 | |
| 197 | 260 | if navigationAction.navigationType == .other{ |
| 198 | 261 | //跳转Appstore | ... | ... |