AppDelegate.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  28. // Override point for customization after application launch.
  29. GMSServices.provideAPIKey("AIzaSyBfIiBM_YSBlxybmI_Uz_fGoUFN4wacR80")
  30. return true
  31. }
  32. /**
  33. Sent when the application is about to move from active to inactive state.
  34. This can occur for certain types of temporary interruptions
  35. (such as an incoming phone call or SMS message) or when the user quits
  36. the application and it begins the transition to the background state.
  37. Use this method to pause ongoing tasks, disable timers, and invalidate
  38. graphics rendering callbacks. Games should use this method to pause the game.
  39. */
  40. func applicationWillResignActive(_ application: UIApplication) {
  41. }
  42. /**
  43. Use this method to release shared resources, save user data, invalidate
  44. timers, and store enough application state information to restore your
  45. application to its current state in case it is terminated later.
  46. If your application supports background execution, this method is called
  47. instead of applicationWillTerminate: when the user quits.
  48. */
  49. func applicationDidEnterBackground(_ application: UIApplication) {
  50. }
  51. /**
  52. Called as part of the transition from the background to the active state;
  53. here you can undo many of the changes made on entering the background.
  54. */
  55. func applicationWillEnterForeground(_ application: UIApplication) {
  56. }
  57. /**
  58. Restart any tasks that were paused (or not yet started) while the
  59. application was inactive. If the application was previously in the background,
  60. optionally refresh the user interface.
  61. */
  62. func applicationDidBecomeActive(_ application: UIApplication) {
  63. }
  64. /**
  65. Called when the application is about to terminate. Save data if appropriate.
  66. See also applicationDidEnterBackground:.
  67. Saves changes in the application's managed object context before the application terminates.
  68. */
  69. func applicationWillTerminate(_ application: UIApplication) {
  70. }
  71. // MARK: - Core Data stack
  72. // The directory the application uses to store the Core Data store file.
  73. lazy var applicationDocumentsDirectory: URL = {
  74. let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
  75. return urls[urls.count-1]
  76. }()
  77. // The managed object model for the application
  78. lazy var managedObjectModel: NSManagedObjectModel = {
  79. let modelURL = Bundle.main.url(forResource: "app", withExtension: "momd")!
  80. return NSManagedObjectModel(contentsOf: modelURL)!
  81. }()
  82. // he persistent store coordinator for the application
  83. lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
  84. let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
  85. let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
  86. var failureReason = "There was an error creating or loading the application's saved data."
  87. do {
  88. try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
  89. } catch {
  90. // Report any error we got.
  91. var dict = [String: AnyObject]()
  92. dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
  93. dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
  94. dict[NSUnderlyingErrorKey] = error as NSError
  95. let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
  96. NSLog(":DELEGATE:ERROR: Unresolved error \(wrappedError), \(wrappedError.userInfo)")
  97. abort()
  98. }
  99. return coordinator
  100. }()
  101. // Returns the managed object context for the application
  102. lazy var managedObjectContext: NSManagedObjectContext = {
  103. let coordinator = self.persistentStoreCoordinator
  104. var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  105. managedObjectContext.persistentStoreCoordinator = coordinator
  106. return managedObjectContext
  107. }()
  108. // MARK: - Core Data Saving support
  109. /**
  110. Saves the application context.
  111. */
  112. func saveContext () {
  113. if managedObjectContext.hasChanges {
  114. do {
  115. try managedObjectContext.save()
  116. } catch {
  117. // Replace this implementation with code to handle the error appropriately.
  118. // 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.
  119. let nserror = error as NSError
  120. NSLog(":DELEGATE:ERROR: Unresolved error \(nserror), \(nserror.userInfo)")
  121. abort()
  122. }
  123. }
  124. }
  125. }