AppDelegate.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // AppDelegate.swift
  3. // LoopFollow
  4. //
  5. // Created by Jon Fawcett on 6/1/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import UIKit
  9. import CoreData
  10. import UserNotifications
  11. import EventKit
  12. @UIApplicationMain
  13. class AppDelegate: UIResponder, UIApplicationDelegate {
  14. var window: UIWindow?
  15. let notificationCenter = UNUserNotificationCenter.current()
  16. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  17. // Override point for customization after application launch.
  18. let options: UNAuthorizationOptions = [.alert, .sound, .badge]
  19. notificationCenter.requestAuthorization(options: options) {
  20. (didAllow, error) in
  21. if !didAllow {
  22. print("User has declined notifications")
  23. }
  24. }
  25. let store = EKEventStore()
  26. store.requestAccess(to: .event) {(granted, error) in
  27. if !granted { return }
  28. }
  29. UNUserNotificationCenter.current().delegate = self
  30. return true
  31. }
  32. func applicationWillTerminate(_ application: UIApplication) {
  33. if UserDefaultsRepository.alertAppInactive.value {
  34. AlarmSound.setSoundFile(str: "Alarm_Buzzer")
  35. AlarmSound.playTerminated()
  36. }
  37. }
  38. // MARK: UISceneSession Lifecycle
  39. func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  40. // This application should be called in background every X Minutes
  41. UIApplication.shared.setMinimumBackgroundFetchInterval(
  42. TimeInterval(UserDefaultsRepository.backgroundRefreshFrequency.value * 60)
  43. )
  44. // set "prevent screen lock" to ON when the app is started for the first time
  45. if !UserDefaultsRepository.screenlockSwitchState.exists {
  46. UserDefaultsRepository.screenlockSwitchState.value = true
  47. }
  48. // set the "prevent screen lock" option when the app is started
  49. // This method doesn't seem to be working anymore. Added to view controllers as solution offered on SO
  50. UIApplication.shared.isIdleTimerDisabled = UserDefaultsRepository.screenlockSwitchState.value
  51. return true
  52. }
  53. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  54. // Called when a new scene session is being created.
  55. // Use this method to select a configuration to create the new scene with.
  56. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  57. }
  58. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  59. // Called when the user discards a scene session.
  60. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  61. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  62. }
  63. // MARK: - Core Data stack
  64. lazy var persistentContainer: NSPersistentCloudKitContainer = {
  65. /*
  66. The persistent container for the application. This implementation
  67. creates and returns a container, having loaded the store for the
  68. application to it. This property is optional since there are legitimate
  69. error conditions that could cause the creation of the store to fail.
  70. */
  71. let container = NSPersistentCloudKitContainer(name: "LoopFollow")
  72. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  73. if let error = error as NSError? {
  74. // Replace this implementation with code to handle the error appropriately.
  75. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  76. /*
  77. Typical reasons for an error here include:
  78. * The parent directory does not exist, cannot be created, or disallows writing.
  79. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  80. * The device is out of space.
  81. * The store could not be migrated to the current model version.
  82. Check the error message to determine what the actual problem was.
  83. */
  84. fatalError("Unresolved error \(error), \(error.userInfo)")
  85. }
  86. })
  87. return container
  88. }()
  89. // MARK: - Core Data Saving support
  90. func saveContext () {
  91. let context = persistentContainer.viewContext
  92. if context.hasChanges {
  93. do {
  94. try context.save()
  95. } catch {
  96. // Replace this implementation with code to handle the error appropriately.
  97. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  98. let nserror = error as NSError
  99. fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  100. }
  101. }
  102. }
  103. }
  104. extension AppDelegate: UNUserNotificationCenterDelegate {
  105. func userNotificationCenter(_ center: UNUserNotificationCenter,
  106. willPresent notification: UNNotification,
  107. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
  108. {
  109. completionHandler(.alert)
  110. }
  111. }