AppDelegate.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. return true
  30. }
  31. func applicationWillTerminate(_ application: UIApplication) {
  32. if UserDefaultsRepository.alertAppInactive.value {
  33. AlarmSound.setSoundFile(str: "Alarm_Buzzer")
  34. AlarmSound.playTerminated()
  35. }
  36. }
  37. // MARK: UISceneSession Lifecycle
  38. func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  39. // This application should be called in background every X Minutes
  40. UIApplication.shared.setMinimumBackgroundFetchInterval(
  41. TimeInterval(UserDefaultsRepository.backgroundRefreshFrequency.value * 60)
  42. )
  43. // set "prevent screen lock" to ON when the app is started for the first time
  44. if !UserDefaultsRepository.screenlockSwitchState.exists {
  45. UserDefaultsRepository.screenlockSwitchState.value = true
  46. }
  47. // set the "prevent screen lock" option when the app is started
  48. // This method doesn't seem to be working anymore. Added to view controllers as solution offered on SO
  49. UIApplication.shared.isIdleTimerDisabled = UserDefaultsRepository.screenlockSwitchState.value
  50. return true
  51. }
  52. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  53. // Called when a new scene session is being created.
  54. // Use this method to select a configuration to create the new scene with.
  55. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  56. }
  57. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  58. // Called when the user discards a scene session.
  59. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  60. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  61. }
  62. // MARK: - Core Data stack
  63. lazy var persistentContainer: NSPersistentCloudKitContainer = {
  64. /*
  65. The persistent container for the application. This implementation
  66. creates and returns a container, having loaded the store for the
  67. application to it. This property is optional since there are legitimate
  68. error conditions that could cause the creation of the store to fail.
  69. */
  70. let container = NSPersistentCloudKitContainer(name: "LoopFollow")
  71. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  72. if let error = error as NSError? {
  73. // Replace this implementation with code to handle the error appropriately.
  74. // 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.
  75. /*
  76. Typical reasons for an error here include:
  77. * The parent directory does not exist, cannot be created, or disallows writing.
  78. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  79. * The device is out of space.
  80. * The store could not be migrated to the current model version.
  81. Check the error message to determine what the actual problem was.
  82. */
  83. fatalError("Unresolved error \(error), \(error.userInfo)")
  84. }
  85. })
  86. return container
  87. }()
  88. // MARK: - Core Data Saving support
  89. func saveContext () {
  90. let context = persistentContainer.viewContext
  91. if context.hasChanges {
  92. do {
  93. try context.save()
  94. } catch {
  95. // Replace this implementation with code to handle the error appropriately.
  96. // 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.
  97. let nserror = error as NSError
  98. fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  99. }
  100. }
  101. }
  102. }