BackyardViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // BackyardViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/20.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import WebKit
  11. import WebViewJavascriptBridge
  12. import SwiftyJSON
  13. import MJRefresh
  14. class BackyardViewController: BaseJSWebViewController {
  15. deinit {
  16. NXLLog("deinit")
  17. if observe != nil {
  18. NotificationCenter.default.removeObserver(observe!)
  19. }
  20. }
  21. weak var observe : NSObjectProtocol?
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. self.URLString = kH5PlanetUrl
  25. navigationBar.isHidden = true
  26. setupViews()
  27. setupData()
  28. }
  29. override func viewWillAppear(_ animated: Bool) {
  30. super.viewWillAppear(animated)
  31. navigationController?.navigationBar.isHidden = true
  32. if UserModel.isTokenNil() {
  33. statusBarStyle = .default
  34. }else {
  35. statusBarStyle = .lightContent
  36. }
  37. }
  38. override func didReceiveMemoryWarning() {
  39. super.didReceiveMemoryWarning()
  40. // Dispose of any resources that can be recreated.
  41. }
  42. //MARK: - view
  43. override func setupViews() {
  44. super.setupViews()
  45. self.wkWebView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-kTabBarTotalHeight)
  46. self.wkWebView.navigationDelegate = self
  47. }
  48. override func setupData() {
  49. super.setupData()
  50. WebViewJavascriptBridgeManager.shared.callHandlerH5(bridge: bridge)
  51. WebViewJavascriptBridgeManager.shared.registerHandlerApp(bridge: bridge) { (wvjbModel) in
  52. WebViewJavascriptBridgeManager.shared.jump(wvjbModel: wvjbModel)
  53. }
  54. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  55. if notification.userInfo == nil {
  56. WebViewJavascriptBridgeManager.shared.callHandlerH5(bridge: self?.bridge)
  57. }
  58. }
  59. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("login"), object: nil, queue: OperationQueue.main, using: {
  60. [weak self] (notification) in
  61. WebViewJavascriptBridgeManager.shared.callHandlerH5(bridge: self?.bridge)
  62. })
  63. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("loginOut"), object: nil, queue: OperationQueue.main, using: {
  64. [weak self] (notification) in
  65. WebViewJavascriptBridgeManager.shared.callHandlerH5(bridge: self?.bridge)
  66. })
  67. //将要进入前台的时候
  68. observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
  69. [weak self] (notification) in
  70. WebViewJavascriptBridgeManager.shared.callHandlerH5(bridge: self?.bridge)
  71. })
  72. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("BackyardViewModuleTop"), object: nil, queue: OperationQueue.main, using: {
  73. [weak self] (notification) in
  74. var silent = notification.object as? Int
  75. if silent == nil {
  76. silent = 0
  77. }
  78. WebViewJavascriptBridgeManager.shared.callHandlerH5Refresh(bridge: self?.bridge, refreshModelType: .planet,silent:silent)
  79. })
  80. }
  81. }
  82. extension BackyardViewController : WKNavigationDelegate {
  83. //服务器开始请求的时候调用
  84. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  85. let navigationURL = navigationAction.request.url?.absoluteString
  86. if let requestURL = navigationURL?.removingPercentEncoding {
  87. //拨打电话
  88. //兼容安卓的服务器写法:<a class = "mobile" href = "tel://电话号码"></a>
  89. //或者:<a class = "mobile" href = "tel:电话号码"></a>
  90. if requestURL.hasPrefix("tel://") {
  91. //取消WKWebView 打电话请求
  92. decisionHandler(.cancel);
  93. //用openURL 这个API打电话
  94. if let mobileURL:URL = URL(string: requestURL) {
  95. if #available(iOS 10, *) {
  96. UIApplication.shared.open(mobileURL, options: [:], completionHandler: nil)
  97. } else {
  98. UIApplication.shared.openURL(mobileURL)
  99. }
  100. }
  101. }
  102. }
  103. switch navigationAction.navigationType {
  104. case WKNavigationType.linkActivated:
  105. pushCurrentSnapshotView(navigationAction.request as NSURLRequest)
  106. break
  107. case WKNavigationType.formSubmitted:
  108. pushCurrentSnapshotView(navigationAction.request as NSURLRequest)
  109. break
  110. case WKNavigationType.backForward:
  111. break
  112. case WKNavigationType.reload:
  113. break
  114. case WKNavigationType.formResubmitted:
  115. break
  116. case WKNavigationType.other:
  117. pushCurrentSnapshotView(navigationAction.request as NSURLRequest)
  118. break
  119. @unknown default:
  120. break
  121. }
  122. decisionHandler(.allow)
  123. }
  124. //开始加载
  125. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  126. }
  127. //这个是网页加载完成,导航的变化
  128. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  129. }
  130. //跳转失败的时候调用
  131. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  132. }
  133. // 内容加载失败时候调用
  134. func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  135. progressView.isHidden = true
  136. }
  137. // 打开新窗口委托
  138. func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
  139. if navigationAction.targetFrame?.isMainFrame == nil {
  140. webView.load(navigationAction.request)
  141. }
  142. return nil
  143. }
  144. }