AppDelegate.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import UIKit
  21. import CoreData
  22. import GoogleMaps
  23. @UIApplicationMain
  24. class AppDelegate: UIResponder, UIApplicationDelegate {
  25. var window: UIWindow?
  26. var controller: ViewController?
  27. var albumController: AlbumViewController?
  28. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  29. // Override point for customization after application launch.
  30. // Google Mapas API KEY
  31. GMSServices.provideAPIKey("AIzaSyBfIiBM_YSBlxybmI_Uz_fGoUFN4wacR80")
  32. // Background mode.
  33. UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
  34. return true
  35. }
  36. func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  37. NSLog(":BACKGROUND:LOG: Start bg sync...")
  38. Sync()
  39. let when = DispatchTime.now() + 120 // 2 minutes to perform the sync.
  40. DispatchQueue.main.asyncAfter(deadline: when) {
  41. NSLog(":BACKGROUND:LOG: Calling completion handler.")
  42. completionHandler(UIBackgroundFetchResult.newData)
  43. }
  44. /*if let tabBarController = window?.rootViewController as? UITabBarController, let viewControllers = tabBarController.viewControllers {
  45. for viewController in viewControllers {
  46. if let fetchViewController = viewController as? FetchViewController {
  47. fetchViewController.fetch {
  48. fetchViewController.updateUI()
  49. completionHandler(.newData)
  50. }
  51. }
  52. }
  53. }*/
  54. }
  55. /**
  56. Sent when the application is about to move from active to inactive state.
  57. This can occur for certain types of temporary interruptions
  58. (such as an incoming phone call or SMS message) or when the user quits
  59. the application and it begins the transition to the background state.
  60. Use this method to pause ongoing tasks, disable timers, and invalidate
  61. graphics rendering callbacks. Games should use this method to pause the game.
  62. */
  63. func applicationWillResignActive(_ application: UIApplication) {
  64. }
  65. /**
  66. Use this method to release shared resources, save user data, invalidate
  67. timers, and store enough application state information to restore your
  68. application to its current state in case it is terminated later.
  69. If your application supports background execution, this method is called
  70. instead of applicationWillTerminate: when the user quits.
  71. */
  72. func applicationDidEnterBackground(_ application: UIApplication) {
  73. }
  74. /**
  75. Called as part of the transition from the background to the active state;
  76. here you can undo many of the changes made on entering the background.
  77. */
  78. func applicationWillEnterForeground(_ application: UIApplication) {
  79. }
  80. /**
  81. Restart any tasks that were paused (or not yet started) while the
  82. application was inactive. If the application was previously in the background,
  83. optionally refresh the user interface.
  84. */
  85. func applicationDidBecomeActive(_ application: UIApplication) {
  86. }
  87. /**
  88. Called when the application is about to terminate. Save data if appropriate.
  89. See also applicationDidEnterBackground:.
  90. Saves changes in the application's managed object context before the application terminates.
  91. */
  92. func applicationWillTerminate(_ application: UIApplication) {
  93. }
  94. // MARK: - Core Data stack
  95. // The directory the application uses to store the Core Data store file.
  96. lazy var applicationDocumentsDirectory: URL = {
  97. let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
  98. return urls[urls.count-1]
  99. }()
  100. // The managed object model for the application
  101. lazy var managedObjectModel: NSManagedObjectModel = {
  102. let modelURL = Bundle.main.url(forResource: "app", withExtension: "momd")!
  103. return NSManagedObjectModel(contentsOf: modelURL)!
  104. }()
  105. // he persistent store coordinator for the application
  106. lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
  107. let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
  108. let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
  109. var failureReason = "There was an error creating or loading the application's saved data."
  110. do {
  111. try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
  112. } catch {
  113. // Report any error we got.
  114. var dict = [String: AnyObject]()
  115. dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
  116. dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
  117. dict[NSUnderlyingErrorKey] = error as NSError
  118. let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
  119. NSLog(":DELEGATE:ERROR: Unresolved error \(wrappedError), \(wrappedError.userInfo)")
  120. abort()
  121. }
  122. return coordinator
  123. }()
  124. // Returns the managed object context for the application
  125. lazy var managedObjectContext: NSManagedObjectContext = {
  126. let coordinator = self.persistentStoreCoordinator
  127. var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  128. managedObjectContext.persistentStoreCoordinator = coordinator
  129. return managedObjectContext
  130. }()
  131. // MARK: - Core Data Saving support
  132. /**
  133. Saves the application context.
  134. */
  135. func saveContext () {
  136. if managedObjectContext.hasChanges {
  137. do {
  138. try managedObjectContext.save()
  139. } catch {
  140. // Replace this implementation with code to handle the error appropriately.
  141. // abort() 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.
  142. let nserror = error as NSError
  143. NSLog(":DELEGATE:ERROR: Unresolved error \(nserror), \(nserror.userInfo)")
  144. abort()
  145. }
  146. }
  147. }
  148. }