AppDelegate.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // AppDelegate.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. import AMapFoundationKit
  9. import IQKeyboardManagerSwift
  10. import UserNotifications
  11. @main
  12. class AppDelegate: UIResponder, UIApplicationDelegate {
  13. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  14. // Override point for customization after application launch.
  15. QSLTools.shared.isOpen = false
  16. UIApplication.shared.shortcutItems = []
  17. Thread.sleep(forTimeInterval: 0.5)
  18. IQKeyboardManager.shared.enable = true
  19. AMapServices.shared().apiKey = QSLConfig.MapKey
  20. QSLDeviceTool.shared.configure(autoRefresh: true)
  21. configNoti()
  22. return true
  23. }
  24. func configNoti(){
  25. UNUserNotificationCenter.current().delegate = self
  26. QSLTools.shared.requestNotificationPermission()
  27. }
  28. // MARK: UISceneSession Lifecycle
  29. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  30. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  31. }
  32. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  33. }
  34. // 当 App 即将终止(退出)时调用
  35. func applicationWillTerminate(_ application: UIApplication) {
  36. print("App 即将退出")
  37. // 仅在 App 被用户主动退出或系统正常终止时触发(强制杀死进程时可能不调用)
  38. // 可在此做最后的数据保存(操作需快速完成,系统可能限制时间)
  39. ///埋点app退出
  40. QSEventHandle.gravityPush(eventName: QSLGravityConst.app_exit, eventProps: ["id": 02002])
  41. }
  42. }
  43. extension AppDelegate : UNUserNotificationCenterDelegate{
  44. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  45. let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
  46. UserDefaults.standard.set(token, forKey: "deviceToken")
  47. UIPasteboard.general.string = token
  48. print("notification token: \(token)")
  49. QSLNetwork().request(.notiTokenReport(dict: ["deviceToken":token])) { response in
  50. } fail: { code, error in
  51. }
  52. }
  53. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  54. let userInfo = notification.request.content.userInfo
  55. print("Foreground notification received: \(userInfo)")
  56. if(userInfo.keys.contains("urlScheme")){
  57. dealwithUserifo(scheme: userInfo["urlScheme"] as! String)
  58. }
  59. // 显示通知
  60. completionHandler([.alert, .sound, .badge])
  61. }
  62. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  63. let userInfo = response.notification.request.content.userInfo
  64. print("Notification tapped: \(userInfo)")
  65. if(userInfo.keys.contains("urlScheme")){
  66. dealwithUserifo(scheme: userInfo["urlScheme"] as! String)
  67. }
  68. completionHandler()
  69. }
  70. func dealwithUserifo(scheme: String){
  71. //这里要判断是否已经处于首页 如果在首页直接打开 不在首页等进首页再打开
  72. if(QSLTools.shared.isOpen){
  73. //处理urlScheme
  74. QSLTools.shared.urlScheme = ""
  75. QSLTools.shared.dealwithScheme(scheme)
  76. return
  77. }
  78. QSLTools.shared.urlScheme = scheme
  79. }
  80. }