AppDelegate.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // AppDelegate.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/6/14.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import AlamofireNetworkActivityIndicator
  11. import UserNotifications
  12. import Flutter
  13. import IQKeyboardManagerSwift
  14. let isProduction = true
  15. @UIApplicationMain
  16. class AppDelegate: FlutterAppDelegate, JPUSHRegisterDelegate, UNUserNotificationCenterDelegate {
  17. var _mapManager: BMKMapManager?
  18. //中心服务器节点类
  19. public static let o2Collect = O2Collect()
  20. //网络监听
  21. public let o2ReachabilityManager = O2ReachabilityManager.sharedInstance
  22. // flutter engine
  23. var flutterEngine : FlutterEngine?
  24. override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  25. if #available(iOS 13.0, *) {
  26. window.overrideUserInterfaceStyle = .light
  27. }
  28. let themeName = AppConfigSettings.shared.themeName
  29. if themeName != "" {
  30. //主题
  31. print("主题色:\(themeName)")
  32. O2ThemeManager.setTheme(plistName: themeName, path: .mainBundle)
  33. }else {
  34. O2ThemeManager.setTheme(plistName: "red", path: .mainBundle)
  35. }
  36. //搜索框
  37. UISearchBar.appearance().theme_barTintColor = ThemeColorPicker(keyPath: "Base.base_color")
  38. UISearchBar.appearance().tintColor = UIColor.white
  39. UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).theme_tintColor = ThemeColorPicker(keyPath: "Base.base_color")
  40. //启动日志管理器
  41. O2Logger.startLogManager()
  42. //日志文件
  43. _ = O2Logger.getLogFiles()
  44. O2Logger.debug("设置运行版本==========,\(PROJECTMODE)")
  45. //网络检查
  46. o2ReachabilityManager.startListening()
  47. //Alamofire
  48. NetworkActivityIndicatorManager.shared.isEnabled = true
  49. //db
  50. let _ = DBManager.shared
  51. //设置一个是否第一授权的标志
  52. if #available(iOS 10.0, *){
  53. let center = UNUserNotificationCenter.current()
  54. center.delegate = self
  55. let options:UNAuthorizationOptions = [.badge,.alert,.sound]
  56. center.requestAuthorization(options: options, completionHandler: { (granted, err) in
  57. if granted == true {
  58. //记录已经打开授权
  59. //print("aaaaaaaaaaaa")
  60. AppConfigSettings.shared.notificationGranted = true
  61. AppConfigSettings.shared.firstGranted = true
  62. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  63. }else{
  64. //记录禁用授权
  65. AppConfigSettings.shared.notificationGranted = false
  66. AppConfigSettings.shared.firstGranted = true
  67. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  68. }
  69. })
  70. }else{
  71. let types:UIUserNotificationType = [.badge,.alert,.sound]
  72. let setting = UIUserNotificationSettings(types: types, categories: nil)
  73. UIApplication.shared.registerUserNotificationSettings(setting)
  74. }
  75. //Buglyy异常上报
  76. Bugly.start(withAppId: BUGLY_ID)
  77. //JPush
  78. _setupJPUSH()
  79. JPUSHService.setup(withOption: launchOptions, appKey: JPUSH_APP_KEY, channel: JPUSH_channel, apsForProduction: isProduction)
  80. _mapManager = BMKMapManager()
  81. BMKMapManager.setCoordinateTypeUsedInBaiduMapSDK(.COORDTYPE_BD09LL)
  82. _mapManager?.start(BAIDU_MAP_KEY, generalDelegate: nil)
  83. JPUSHService.registrationIDCompletionHandler { (resCode, registrationID) in
  84. if resCode == 0 {
  85. O2Logger.debug("registrationID获取成功\(registrationID ?? "")")
  86. O2AuthSDK.shared.setDeviceToken(token: registrationID ?? "registrationIDerror0x0x")
  87. }else{
  88. O2Logger.debug("registrationID获取失败,code:\(resCode)")
  89. O2AuthSDK.shared.setDeviceToken(token: registrationID ?? "registrationIDerror0x0x")
  90. }
  91. }
  92. // OOPlusButtonSubclass.register()
  93. OOTabBarHelper.initTabBarStyle()
  94. //
  95. IQKeyboardManager.shared.enable = true
  96. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  97. }
  98. // MARK:- private func Jpush
  99. private func _setupJPUSH() {
  100. let entity = JPUSHRegisterEntity()
  101. entity.types = NSInteger(UNAuthorizationOptions.alert.rawValue) |
  102. NSInteger(UNAuthorizationOptions.sound.rawValue) |
  103. NSInteger(UNAuthorizationOptions.badge.rawValue)
  104. JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
  105. }
  106. //注册 APNs 获得device token
  107. override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  108. super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
  109. NotificationCenter.default.post(name: Notification.Name(rawValue: "DidRegisterRemoteNotification"), object: deviceToken)
  110. JPUSHService.registerDeviceToken(deviceToken)
  111. }
  112. override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  113. DDLogDebug("open url :\(url.absoluteString)")
  114. return true
  115. }
  116. override func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
  117. if notificationSettings.types.rawValue == 0 {
  118. AppConfigSettings.shared.notificationGranted = false
  119. AppConfigSettings.shared.firstGranted = true
  120. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  121. }else{
  122. AppConfigSettings.shared.notificationGranted = true
  123. AppConfigSettings.shared.firstGranted = true
  124. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  125. }
  126. }
  127. //实现注册 APNs 失败接口
  128. override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  129. DDLogError("didFailToRegisterForRemoteNotificationsWithError: \(error.localizedDescription)")
  130. }
  131. override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  132. JPUSHService.handleRemoteNotification(userInfo)
  133. DDLogDebug("收到通知,\(userInfo)")
  134. NotificationCenter.default.post(name: Notification.Name(rawValue: "AddNotificationCount"), object: nil)
  135. }
  136. override func applicationWillEnterForeground(_ application: UIApplication) {
  137. application.applicationIconBadgeNumber = 0
  138. application.cancelAllLocalNotifications()
  139. }
  140. override func applicationDidBecomeActive(_ application: UIApplication) {
  141. }
  142. override func applicationDidEnterBackground(_ application: UIApplication) {
  143. application.applicationIconBadgeNumber = 0
  144. application.cancelAllLocalNotifications()
  145. }
  146. deinit {
  147. o2ReachabilityManager.stopListening()
  148. }
  149. // iOS 12 Support
  150. @available(iOS 12.0, *)
  151. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) {
  152. // open
  153. if (notification != nil && (notification?.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) ) {
  154. //从通知界面直接进入应用
  155. DDLogInfo("从通知界面直接进入应用............")
  156. }else{
  157. //从通知设置界面进入应用
  158. DDLogInfo("从通知设置界面进入应用............")
  159. }
  160. }
  161. @available(iOS 10.0, *)
  162. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!,
  163. withCompletionHandler completionHandler: ((Int) -> Void)!) {
  164. let userInfo = notification.request.content.userInfo
  165. let request = notification.request // 收到推送的请求
  166. let content = request.content // 收到推送的消息内容
  167. let badge = content.badge // 推送消息的角标
  168. let body = content.body // 推送消息体
  169. let sound = content.sound // 推送消息的声音
  170. let subtitle = content.subtitle // 推送消息的副标题
  171. let title = content.title // 推送消息的标题
  172. if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  173. JPUSHService.handleRemoteNotification(userInfo)
  174. }else{
  175. //判断为本地通知
  176. O2Logger.debug("iOS10 前台收到本地通知:{\nbody:\(body),\ntitle:\(title),\nsubtitle:\(subtitle),\nbadge:\(badge ?? 0),\nsound:\(sound.debugDescription)")
  177. }
  178. UIApplication.shared.applicationIconBadgeNumber = 0
  179. JPUSHService.setBadge(0)
  180. completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue|UNNotificationPresentationOptions.sound.rawValue))
  181. }
  182. @available(iOS 10.0, *)
  183. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
  184. let userInfo = response.notification.request.content.userInfo
  185. let request = response.notification.request // 收到推送的请求
  186. let content = request.content // 收到推送的消息内容
  187. let badge = content.badge // 推送消息的角标
  188. let body = content.body // 推送消息体
  189. let sound = content.sound // 推送消息的声音
  190. let subtitle = content.subtitle // 推送消息的副标题
  191. let title = content.title // 推送消息的标题
  192. if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  193. JPUSHService.handleRemoteNotification(userInfo)
  194. }else{
  195. //判断为本地通知
  196. O2Logger.debug("iOS10 前台收到本地通知:{\nbody:\(body),\ntitle:\(title),\nsubtitle:\(subtitle),\nbadge:\(badge ?? 0),\nsound:\(sound.debugDescription)")
  197. }
  198. UIApplication.shared.applicationIconBadgeNumber = 0
  199. JPUSHService.setBadge(0)
  200. completionHandler()
  201. }
  202. }