Commit 5ca9e19b6127c06db600d55ea8e542955a5a1edc
1 parent
1be46733
网页加交互, ...分享 , 网页返回上一步
Showing
7 changed files
with
175 additions
and
19 deletions
Podfile
metaCode/Classes/Common/Base/CNLiveBaseViewController.swift
| ... | ... | @@ -108,7 +108,9 @@ class CNLiveBaseViewController: UIViewController { |
| 108 | 108 | btn.frame = CGRect(x: lastBtn != nil ? (lastBtn!.origin.x - btn.width) : (topNavView.width - CNLiveBaseViewController.kTopNavLeftRightOffset - btn.width), y: topNavViewHeight - btn.height, width: btn.width, height: btn.height) |
| 109 | 109 | lastBtn = btn |
| 110 | 110 | } |
| 111 | - rightNavSpace = CGRectGetMaxX(lastBtn!.frame) | |
| 111 | + if _rightNavBtns.count > 0{ | |
| 112 | + rightNavSpace = CGRectGetMaxX(lastBtn!.frame) | |
| 113 | + } | |
| 112 | 114 | viewWillLayoutSubviews() |
| 113 | 115 | } |
| 114 | 116 | } | ... | ... |
metaCode/Classes/Main/HomePage/Controller/WebController/CNWebViewController.swift
| ... | ... | @@ -7,17 +7,41 @@ |
| 7 | 7 | |
| 8 | 8 | import Foundation |
| 9 | 9 | import WebKit |
| 10 | +import WebViewJavascriptBridge | |
| 11 | +import CNLiveShareToolKit | |
| 10 | 12 | |
| 11 | 13 | class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 12 | 14 | |
| 15 | + var bridge:WKWebViewJavascriptBridge? | |
| 16 | + var currentUrl:String = ""//当前网页的url | |
| 17 | + | |
| 13 | 18 | lazy var webView: WKWebView = { |
| 14 | - let webView = WKWebView.init(frame: CGRectMake(0, 0, Width, Height)) | |
| 19 | + let configuration = WKWebViewConfiguration() | |
| 20 | + configuration.allowsInlineMediaPlayback = true | |
| 21 | + let preferences = WKPreferences() | |
| 22 | + preferences.javaScriptCanOpenWindowsAutomatically = true | |
| 23 | + preferences.javaScriptEnabled = true | |
| 24 | + configuration.preferences.minimumFontSize = 0.0 | |
| 25 | + configuration.preferences = preferences | |
| 26 | + configuration.userContentController = WKUserContentController() | |
| 27 | + | |
| 28 | + let webView = WKWebView(frame: CGRectMake(0, 0, Width, Height), configuration: configuration) | |
| 15 | 29 | webView.scrollView.showsVerticalScrollIndicator = false |
| 16 | 30 | webView.scrollView.showsHorizontalScrollIndicator = false |
| 17 | - webView.navigationDelegate = self | |
| 31 | +// webView.navigationDelegate = self | |
| 18 | 32 | return webView |
| 19 | 33 | }() |
| 20 | 34 | |
| 35 | + lazy var moreNavBtn: UIButton = { | |
| 36 | + let moreNavBtn = UIButton(type: .custom) | |
| 37 | + moreNavBtn.frame = CGRect(x: 0, y: KStatusBarHeight, width: KNavigationHeight-KStatusBarHeight, height: KNavigationHeight-KStatusBarHeight) | |
| 38 | + moreNavBtn.backgroundColor = .clear | |
| 39 | + moreNavBtn.setImage(UIImage(named: "web_more"), for: .normal) | |
| 40 | + moreNavBtn.addTarget(self, action: #selector(moreNavBtnAction), for: .touchUpInside) | |
| 41 | + return moreNavBtn | |
| 42 | + }() | |
| 43 | + | |
| 44 | + | |
| 21 | 45 | var _url:URL? |
| 22 | 46 | var url:URL? { |
| 23 | 47 | get{ |
| ... | ... | @@ -26,17 +50,18 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 26 | 50 | set{ |
| 27 | 51 | _url = newValue! |
| 28 | 52 | if (_url != nil){ |
| 29 | - let webUrl: URL! = _url! | |
| 30 | - webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in | |
| 31 | - let userAgent = result as! String | |
| 32 | - if error == nil && userAgent.count > 0 { | |
| 33 | - if !userAgent.contains(CNLiveEnvironmentManager.shared.base_web_UA) { | |
| 34 | - let newUserAgent = userAgent + CNLiveEnvironmentManager.shared.base_web_UA | |
| 35 | - webView.customUserAgent = newUserAgent | |
| 36 | - } | |
| 37 | - } | |
| 38 | - webView.load(URLRequest.init(url:webUrl)) | |
| 39 | - } | |
| 53 | +// let webUrl: URL! = _url! | |
| 54 | +// webView.load(URLRequest.init(url:webUrl)) | |
| 55 | +// webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in | |
| 56 | +// let userAgent = result as! String | |
| 57 | +// if error == nil && userAgent.count > 0 { | |
| 58 | +// if !userAgent.contains(CNLiveEnvironmentManager.shared.base_web_UA) { | |
| 59 | +// let newUserAgent = userAgent + CNLiveEnvironmentManager.shared.base_web_UA | |
| 60 | +// webView.customUserAgent = newUserAgent | |
| 61 | +// } | |
| 62 | +// } | |
| 63 | +// webView.load(URLRequest.init(url:webUrl)) | |
| 64 | +// } | |
| 40 | 65 | } |
| 41 | 66 | } |
| 42 | 67 | } |
| ... | ... | @@ -49,7 +74,87 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 49 | 74 | make.top.equalTo(get_system_nav_height()) |
| 50 | 75 | make.left.right.bottom.equalToSuperview() |
| 51 | 76 | } |
| 52 | - | |
| 77 | + webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in | |
| 78 | + let userAgent = result as! String | |
| 79 | + if error == nil && userAgent.count > 0 { | |
| 80 | + if !userAgent.contains(CNLiveEnvironmentManager.shared.base_web_UA) { | |
| 81 | + let newUserAgent = userAgent + CNLiveEnvironmentManager.shared.base_web_UA | |
| 82 | + webView.customUserAgent = newUserAgent | |
| 83 | + | |
| 84 | + if (_url != nil){ | |
| 85 | + let webUrl: URL! = _url! | |
| 86 | + webView.load(URLRequest.init(url:webUrl)) | |
| 87 | + } | |
| 88 | + self.configBridge() | |
| 89 | + self.configBridgeRegister() | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + func configBridge(){ | |
| 96 | + WKWebViewJavascriptBridge.enableLogging() | |
| 97 | + self.bridge = WKWebViewJavascriptBridge(for: self.webView) | |
| 98 | + self.bridge?.setWebViewDelegate(self) | |
| 99 | + } | |
| 100 | + //h5拉起原生分享 js call oc | |
| 101 | + func configBridgeRegister() -> Void { | |
| 102 | + self.bridge?.registerHandler("wjjylzgVideoShare", handler: { data, responseCallback in | |
| 103 | +// print("js call oc\(data)") | |
| 104 | + let dic = data as! Dictionary<String, String> | |
| 105 | + 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: self, topImage: [], topTitles: [], platformType: .all) { title in | |
| 106 | + | |
| 107 | + } completerBlock: { resultType, platformType, typeString in | |
| 108 | + if resultType == .succ{ | |
| 109 | + | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + }) | |
| 114 | + } | |
| 115 | + | |
| 116 | + //点击...分享获取h5参数oc call js | |
| 117 | + func configBridgeCallHander() -> Void { | |
| 118 | + self.bridge?.callHandler("wjjClientGetShareParaFromJS", data: ["":""], responseCallback: { data in | |
| 119 | + print("js call oc\(data)") | |
| 120 | + let dic = data as! Dictionary<String, String> | |
| 121 | +// let shareType = dic["shareType"] | |
| 122 | +// let contentIdStr = dic["activityId"] | |
| 123 | + let title = dic["title"] | |
| 124 | + let desc = dic["desc"] | |
| 125 | + let shareImg = dic["shareImg"] | |
| 126 | + | |
| 127 | + var shareUrl = "" | |
| 128 | + if var tempShareUrl = dic["shareUrl"]{ | |
| 129 | + if tempShareUrl == ""{ | |
| 130 | + shareUrl = self.currentUrl | |
| 131 | + }else{ | |
| 132 | + shareUrl = tempShareUrl | |
| 133 | + } | |
| 134 | + }else{ | |
| 135 | + shareUrl = self.currentUrl | |
| 136 | + } | |
| 137 | + | |
| 138 | + CNLiveShareManager.showShareViewWithParam(forShareTitle: title, shareUrl: shareUrl, shareDesc: desc, shareImage: shareImg, screenFull: false, hiddenWjj: true, hiddenQQ: false, hiddenWB: true, hiddenLifeCircle: true, hiddenWechatCircle: false, hiddenWechat: false, hiddenSafari: true, formVC: self, topImage: [], topTitles: [], platformType: .all) { title in | |
| 139 | + | |
| 140 | + } completerBlock: { resultType, platformType, typeString in | |
| 141 | + if resultType == .succ{ | |
| 142 | + | |
| 143 | + } | |
| 144 | + } | |
| 145 | + }) | |
| 146 | + } | |
| 147 | + // MARK: - BtnAction | |
| 148 | + // 内部链接右上角···分享 | |
| 149 | + @objc func moreNavBtnAction(){ | |
| 150 | + self.configBridgeCallHander() | |
| 151 | + } | |
| 152 | + override func ClickBackBtn() { | |
| 153 | + if self.webView.canGoBack{ | |
| 154 | + self.webView.goBack() | |
| 155 | + }else{ | |
| 156 | + super.ClickBackBtn() | |
| 157 | + } | |
| 53 | 158 | } |
| 54 | 159 | |
| 55 | 160 | // MARK: - WKWebViewDelegate |
| ... | ... | @@ -61,6 +166,7 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 61 | 166 | func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { |
| 62 | 167 | print("didCommit") |
| 63 | 168 | } |
| 169 | + | |
| 64 | 170 | // 页面加载完成之后调用 |
| 65 | 171 | func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 66 | 172 | print("didFinish") |
| ... | ... | @@ -76,15 +182,31 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 76 | 182 | } |
| 77 | 183 | // 在收到响应后,决定是否跳转 -> 默认允许 |
| 78 | 184 | func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { |
| 79 | - print("decidePolicyFor") | |
| 185 | + | |
| 186 | + print("decidePolicyFornavigationResponse") | |
| 187 | + | |
| 80 | 188 | //允许跳转 |
| 81 | 189 | decisionHandler(.allow) |
| 82 | 190 | //不允许跳转 |
| 83 | 191 | // decisionHandler(.cancel) |
| 84 | 192 | } |
| 85 | 193 | // 在发送请求之前,决定是否跳转 -> 默认允许 |
| 86 | - @available(iOS 13.0, *) | |
| 87 | - func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) { | |
| 194 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { | |
| 195 | + print("decidePolicyFornavigationAction") | |
| 196 | + | |
| 197 | + let urlStr = (navigationAction.request.url?.absoluteString)! | |
| 198 | + | |
| 199 | + if navigationAction.request.url?.host == "wjjh5.cnlive.com"{ | |
| 200 | + self.currentUrl = urlStr | |
| 201 | + | |
| 202 | + if urlStr.contains("&isWjjReport=true") || urlStr.contains("?isWjjReport=true"){ | |
| 203 | + self.rightNavBtns = [self.moreNavBtn] | |
| 204 | + self.moreNavBtn.isHidden = false | |
| 205 | + }else{ | |
| 206 | + self.moreNavBtn.isHidden = true | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 88 | 210 | |
| 89 | 211 | if navigationAction.navigationType == .other{ |
| 90 | 212 | //跳转Appstore |
| ... | ... | @@ -93,7 +215,10 @@ class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{ |
| 93 | 215 | if UIApplication.shared.canOpenURL(url!) { UIApplication.shared.open(url!, options: [:], completionHandler: nil) |
| 94 | 216 | } |
| 95 | 217 | } |
| 218 | + }else if navigationAction.navigationType == .linkActivated{ | |
| 219 | + | |
| 220 | + | |
| 96 | 221 | } |
| 97 | - decisionHandler(.allow, preferences) | |
| 222 | + decisionHandler(.allow) | |
| 98 | 223 | } |
| 99 | 224 | } | ... | ... |
metaCode/Supporting Files/Assets.xcassets/WebView/Contents.json
0 → 100644
metaCode/Supporting Files/Assets.xcassets/WebView/web_more.imageset/Contents.json
0 → 100644
| 1 | +{ | |
| 2 | + "images" : [ | |
| 3 | + { | |
| 4 | + "idiom" : "universal", | |
| 5 | + "scale" : "1x" | |
| 6 | + }, | |
| 7 | + { | |
| 8 | + "filename" : "web_more@2x.png", | |
| 9 | + "idiom" : "universal", | |
| 10 | + "scale" : "2x" | |
| 11 | + }, | |
| 12 | + { | |
| 13 | + "filename" : "web_more@3x.png", | |
| 14 | + "idiom" : "universal", | |
| 15 | + "scale" : "3x" | |
| 16 | + } | |
| 17 | + ], | |
| 18 | + "info" : { | |
| 19 | + "author" : "xcode", | |
| 20 | + "version" : 1 | |
| 21 | + } | |
| 22 | +} | ... | ... |
metaCode/Supporting Files/Assets.xcassets/WebView/web_more.imageset/web_more@2x.png
0 → 100644
450 Bytes
metaCode/Supporting Files/Assets.xcassets/WebView/web_more.imageset/web_more@3x.png
0 → 100644
931 Bytes