AppDelegate.swift 11 KB

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