AppDelegate.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. IQKeyboardManager.shared.enable = false
  95. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  96. }
  97. // MARK:- private func Jpush
  98. private func _setupJPUSH() {
  99. let entity = JPUSHRegisterEntity()
  100. entity.types = NSInteger(UNAuthorizationOptions.alert.rawValue) |
  101. NSInteger(UNAuthorizationOptions.sound.rawValue) |
  102. NSInteger(UNAuthorizationOptions.badge.rawValue)
  103. JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
  104. }
  105. //注册 APNs 获得device token
  106. override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  107. super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
  108. let deviceTokenStr = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
  109. DDLogDebug("获取到APNs deviceToken \(deviceTokenStr)")
  110. O2AuthSDK.shared.setApnsToken(token: deviceTokenStr)
  111. NotificationCenter.default.post(name: Notification.Name(rawValue: "DidRegisterRemoteNotification"), object: deviceToken)
  112. JPUSHService.registerDeviceToken(deviceToken)
  113. }
  114. override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  115. DDLogDebug("open url :\(url.absoluteString)")
  116. return true
  117. }
  118. override func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
  119. if notificationSettings.types.rawValue == 0 {
  120. AppConfigSettings.shared.notificationGranted = false
  121. AppConfigSettings.shared.firstGranted = true
  122. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  123. }else{
  124. AppConfigSettings.shared.notificationGranted = true
  125. AppConfigSettings.shared.firstGranted = true
  126. NotificationCenter.default.post(name: NSNotification.Name.init("SETTING_NOTI"), object: nil)
  127. }
  128. }
  129. //实现注册 APNs 失败接口
  130. override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  131. DDLogError("didFailToRegisterForRemoteNotificationsWithError: \(error.localizedDescription)")
  132. }
  133. override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  134. JPUSHService.handleRemoteNotification(userInfo)
  135. DDLogDebug("收到通知,\(userInfo)")
  136. NotificationCenter.default.post(name: Notification.Name(rawValue: "AddNotificationCount"), object: nil)
  137. }
  138. override func applicationWillEnterForeground(_ application: UIApplication) {
  139. application.applicationIconBadgeNumber = 0
  140. // application.cancelAllLocalNotifications()
  141. }
  142. override func applicationDidBecomeActive(_ application: UIApplication) {
  143. }
  144. override func applicationDidEnterBackground(_ application: UIApplication) {
  145. application.applicationIconBadgeNumber = 0
  146. // application.cancelAllLocalNotifications()
  147. }
  148. deinit {
  149. o2ReachabilityManager.stopListening()
  150. }
  151. // iOS 12 Support
  152. @available(iOS 12.0, *)
  153. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) {
  154. // open
  155. if (notification != nil && (notification?.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) ) {
  156. //从通知界面直接进入应用
  157. DDLogInfo("从通知界面直接进入应用............")
  158. }else{
  159. //从通知设置界面进入应用
  160. DDLogInfo("从通知设置界面进入应用............")
  161. }
  162. }
  163. @available(iOS 10.0, *)
  164. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!,
  165. withCompletionHandler completionHandler: ((Int) -> Void)!) {
  166. let userInfo = notification.request.content.userInfo
  167. let request = notification.request // 收到推送的请求
  168. let content = request.content // 收到推送的消息内容
  169. let badge = content.badge // 推送消息的角标
  170. let body = content.body // 推送消息体
  171. let sound = content.sound // 推送消息的声音
  172. let subtitle = content.subtitle // 推送消息的副标题
  173. let title = content.title // 推送消息的标题
  174. if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  175. JPUSHService.handleRemoteNotification(userInfo)
  176. }else{
  177. //判断为本地通知
  178. O2Logger.debug("iOS10 前台收到本地通知:{\nbody:\(body),\ntitle:\(title),\nsubtitle:\(subtitle),\nbadge:\(badge ?? 0),\nsound:\(sound.debugDescription)")
  179. }
  180. UIApplication.shared.applicationIconBadgeNumber = 0
  181. JPUSHService.setBadge(0)
  182. completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue|UNNotificationPresentationOptions.sound.rawValue))
  183. }
  184. @available(iOS 10.0, *)
  185. func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
  186. let userInfo = response.notification.request.content.userInfo
  187. let request = response.notification.request // 收到推送的请求
  188. let content = request.content // 收到推送的消息内容
  189. let badge = content.badge // 推送消息的角标
  190. let body = content.body // 推送消息体
  191. let sound = content.sound // 推送消息的声音
  192. let subtitle = content.subtitle // 推送消息的副标题
  193. let title = content.title // 推送消息的标题
  194. if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  195. JPUSHService.handleRemoteNotification(userInfo)
  196. }else{
  197. //判断为本地通知
  198. O2Logger.debug("iOS10 前台收到本地通知:{\nbody:\(body),\ntitle:\(title),\nsubtitle:\(subtitle),\nbadge:\(badge ?? 0),\nsound:\(sound.debugDescription)")
  199. }
  200. UIApplication.shared.applicationIconBadgeNumber = 0
  201. JPUSHService.setBadge(0)
  202. completionHandler()
  203. }
  204. }