OJSWebViewController.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // OJSWebViewController.swift
  3. // OJASwiftKitDemo
  4. //
  5. // Created by luxiaoming on 2017/12/23.
  6. // Copyright © 2017年 luxiaoming. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. class OJSWebViewController: UIViewController {
  11. fileprivate lazy var webView: WKWebView = {
  12. let preferences = WKPreferences()
  13. preferences.minimumFontSize = 48
  14. let config = WKWebViewConfiguration()
  15. config.preferences = preferences
  16. config.userContentController.add(self, name: "loginCallback")
  17. config.userContentController.add(self, name: "shareClick")
  18. let webView = WKWebView(frame: self.view.bounds, configuration: config)
  19. webView.navigationDelegate = self
  20. webView.uiDelegate = self
  21. return webView
  22. }()
  23. }
  24. // MARK: - Lifecycle
  25. extension OJSWebViewController {
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. webView.frame = self.view.bounds
  29. self.view.addSubview(webView)
  30. // webView.configuration.userContentController.add(self, name: "loginCallback")
  31. // webView.configuration.userContentController.add(self, name: "getUid")
  32. // if let url = URL(string: "http://www.duowan.com/mlogin/index.html?from=app") {
  33. // let request = URLRequest(url: url)
  34. // webView.load(request)
  35. // }
  36. // if let url = URL(string: "http://www.duowan.com/applogin/index.html") {
  37. // let request = URLRequest(url: url)
  38. // webView.load(request)
  39. // }
  40. // if let url = URL(string: "http://172.16.15.158:8080/iosTest.html") {
  41. // let request = URLRequest(url: url)
  42. // webView.load(request)
  43. // }
  44. // NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
  45. // NSURL *fileURL = [NSURL fileURLWithPath:urlStr];
  46. // [self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
  47. let urlString = Bundle.main.path(forResource: "index2", ofType: "html")
  48. let fileUrl = URL(fileURLWithPath: urlString!)
  49. if #available(iOS 9.0, *) {
  50. webView.loadFileURL(fileUrl, allowingReadAccessTo: fileUrl)
  51. } else {
  52. // Fallback on earlier versions
  53. }
  54. }
  55. override func didReceiveMemoryWarning() {
  56. super.didReceiveMemoryWarning()
  57. // Dispose of any resources that can be recreated.
  58. }
  59. }
  60. // MARK: - PrivateMethod
  61. private extension OJSWebViewController {
  62. func setupForJavaScriptAction(webView: WKWebView) {
  63. }
  64. func runJSFunc() {
  65. let aaa = "aaa"
  66. let jsString = "setLocation('\(aaa)')"
  67. webView.evaluateJavaScript(jsString) { (result, error) in
  68. if let error = error {
  69. print(error)
  70. } else {
  71. print("result is \(result.debugDescription)")
  72. }
  73. }
  74. }
  75. }
  76. extension OJSWebViewController: WKUIDelegate {
  77. func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
  78. let alert = UIAlertController(title: message, message: message, preferredStyle: .alert)
  79. let cancelActiion = UIAlertAction(title: "ok", style: .cancel, handler: { (action: UIAlertAction) in
  80. completionHandler()
  81. })
  82. alert.addAction(cancelActiion)
  83. self.present(alert, animated: true, completion: nil)
  84. }
  85. }
  86. extension OJSWebViewController: WKNavigationDelegate {
  87. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  88. NSLog(navigation.description)
  89. }
  90. func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
  91. NSLog(error.localizedDescription)
  92. }
  93. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  94. NSLog(error.localizedDescription)
  95. }
  96. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  97. if let url = navigationAction.request.url,
  98. let scheme = url.scheme,
  99. let host = url.host,
  100. scheme == "haleyaction" {
  101. NSLog("\(host)")
  102. if host == "scanClick" {
  103. } else if host == "shareClick" {
  104. } else if (host == "getLocation") {
  105. self.runJSFunc()
  106. } else if (host == "setColor") {
  107. } else if (host == "payAction") {
  108. } else if (host == "shake") {
  109. } else if (host == "goBack") {
  110. }
  111. decisionHandler(.cancel)
  112. return
  113. }
  114. decisionHandler(.allow)
  115. }
  116. }
  117. extension OJSWebViewController: WKScriptMessageHandler {
  118. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  119. if message.name == "loginCallback" {
  120. NSLog("\(message.name): \(message.body)")
  121. }
  122. else if message.name == "shareClick" {
  123. NSLog("\(message.name): \(message.body)")
  124. }
  125. else if message.name == "getUid" {
  126. self.webView.evaluateJavaScript("getUidCallback(\(123))", completionHandler: { (success, error) in
  127. print(success)
  128. print(error)
  129. })
  130. }
  131. }
  132. }