AppDelegate.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // AppDelegate.swift
  3. // test11
  4. //
  5. // Created by Yuji Hato on 4/20/15.
  6. // Copyright (c) 2015 Yuji Hato. All rights reserved.
  7. //
  8. import UIKit
  9. import CoreData
  10. @UIApplicationMain
  11. class AppDelegate: UIResponder, UIApplicationDelegate {
  12. var window: UIWindow?
  13. private func createMenuView() {
  14. // create viewController code...
  15. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  16. let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
  17. let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
  18. let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
  19. UINavigationBar.appearance().tintColor = UIColor(hex: "689F38")
  20. leftViewController.mainViewController = nvc
  21. let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController)
  22. slideMenuController.automaticallyAdjustsScrollViewInsets = true
  23. slideMenuController.delegate = mainViewController
  24. self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
  25. self.window?.rootViewController = slideMenuController
  26. self.window?.makeKeyAndVisible()
  27. }
  28. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  29. self.createMenuView()
  30. return true
  31. }
  32. func applicationWillResignActive(application: UIApplication) {
  33. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  34. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  35. }
  36. func applicationDidEnterBackground(application: UIApplication) {
  37. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  38. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  39. }
  40. func applicationWillEnterForeground(application: UIApplication) {
  41. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  42. }
  43. func applicationDidBecomeActive(application: UIApplication) {
  44. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  45. }
  46. func applicationWillTerminate(application: UIApplication) {
  47. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  48. // Saves changes in the application's managed object context before the application terminates.
  49. self.saveContext()
  50. }
  51. // MARK: - Core Data stack
  52. lazy var applicationDocumentsDirectory: NSURL = {
  53. // The directory the application uses to store the Core Data store file. This code uses a directory named "dekatotoro.test11" in the application's documents Application Support directory.
  54. let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
  55. return urls[urls.count-1]
  56. }()
  57. lazy var managedObjectModel: NSManagedObjectModel = {
  58. // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
  59. let modelURL = NSBundle.mainBundle().URLForResource("test11", withExtension: "momd")!
  60. return NSManagedObjectModel(contentsOfURL: modelURL)!
  61. }()
  62. lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
  63. // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
  64. // Create the coordinator and store
  65. var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
  66. let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("test11.sqlite")
  67. var error: NSError? = nil
  68. var failureReason = "There was an error creating or loading the application's saved data."
  69. do {
  70. try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
  71. } catch var error1 as NSError {
  72. error = error1
  73. coordinator = nil
  74. // Report any error we got.
  75. var dict = [String: AnyObject]()
  76. dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
  77. dict[NSLocalizedFailureReasonErrorKey] = failureReason
  78. dict[NSUnderlyingErrorKey] = error
  79. error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
  80. // Replace this with code to handle the error appropriately.
  81. // 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.
  82. NSLog("Unresolved error \(error), \(error!.userInfo)")
  83. abort()
  84. } catch {
  85. fatalError()
  86. }
  87. return coordinator
  88. }()
  89. lazy var managedObjectContext: NSManagedObjectContext? = {
  90. // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
  91. let coordinator = self.persistentStoreCoordinator
  92. if coordinator == nil {
  93. return nil
  94. }
  95. var managedObjectContext = NSManagedObjectContext()
  96. managedObjectContext.persistentStoreCoordinator = coordinator
  97. return managedObjectContext
  98. }()
  99. // MARK: - Core Data Saving support
  100. func saveContext () {
  101. if let moc = self.managedObjectContext {
  102. var error: NSError? = nil
  103. if moc.hasChanges {
  104. do {
  105. try moc.save()
  106. } catch let error1 as NSError {
  107. error = error1
  108. // Replace this implementation with code to handle the error appropriately.
  109. // 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.
  110. NSLog("Unresolved error \(error), \(error!.userInfo)")
  111. abort()
  112. }
  113. }
  114. }
  115. }
  116. }