O2JsApiNotification.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // O2JsApiNotification.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/4/22.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import CocoaLumberjack
  11. /**
  12. * o2m.notification
  13. **/
  14. class O2JsApiNotification: NSObject, WKScriptMessageHandler {
  15. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  16. if message.body is NSString {
  17. let json = message.body as! NSString
  18. DDLogDebug("message json:\(json)")
  19. if let jsonData = String(json).data(using: .utf8) {
  20. let dicArr = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
  21. let type = dicArr["type"] as! String
  22. switch type {
  23. case "alert":
  24. alert(json: String(json))
  25. break
  26. default:
  27. break
  28. }
  29. }else {
  30. DDLogError("消息json解析异常。。。")
  31. }
  32. }else {
  33. DDLogError("message 消息 body 类型不正确。。。")
  34. }
  35. }
  36. private func alert(json: String) {
  37. // if let alert = O2NotificationMessage<O2NotificationAlertMessage>.deserialize(from: json) {
  38. // var buttonName = alert.data?.buttonName ?? ""
  39. // if buttonName == "" {
  40. // buttonName = "确定"
  41. // }
  42. // let title = alert.data?.title ?? ""
  43. // let message = alert.data?.message ?? "消息"
  44. // self.showSystemAlertWithButtonName(title: title, message: message , buttonName: buttonName) { (action) in
  45. // if alert.callback != nil {
  46. // let callJs = "\(alert.callback!)()"
  47. // DDLogDebug(callJs)
  48. // self.webView.evaluateJavaScript(callJs, completionHandler: { (result, err) in
  49. //
  50. // })
  51. // }
  52. // }
  53. // }else {
  54. // DDLogError("解析json失败")
  55. // self.showError(title: "参数不正确!")
  56. // }
  57. }
  58. }