Guest User

Untitled

a guest
Oct 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. //
  2. // AppDelegate.swift
  3. // Astronomia do dia 2
  4. //
  5. // Created by Jose Lino Neto on 18/02/18.
  6. // Copyright © 2018 Construtor. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreData
  11. import Firebase
  12. import FirebaseUI
  13. import FirebaseMessaging
  14. import UserNotifications
  15.  
  16. @UIApplicationMain
  17. class AppDelegate: UIResponder, UIApplicationDelegate, FUIAuthDelegate, MessagingDelegate {
  18.  
  19. var window: UIWindow?
  20.  
  21.  
  22. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  23. // Override point for customization after application launch.
  24.  
  25. FirebaseApp.configure()
  26. let authUI = FUIAuth.defaultAuthUI()
  27. authUI?.delegate = self
  28.  
  29. // Register Notification
  30. //let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  31. //application.registerUserNotificationSettings(settings)
  32.  
  33. Messaging.messaging().delegate = self
  34. // Register with APNs
  35. self.registerForPushNotifications()
  36.  
  37. // Top navigation white
  38. UIApplication.shared.statusBarStyle = .lightContent
  39.  
  40. return true
  41. }
  42.  
  43. func applicationWillResignActive(_ application: UIApplication) {
  44. // 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.
  45. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  46. }
  47.  
  48. func applicationDidEnterBackground(_ application: UIApplication) {
  49. // 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.
  50. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  51. }
  52.  
  53. func applicationWillEnterForeground(_ application: UIApplication) {
  54. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  55. }
  56.  
  57. func applicationDidBecomeActive(_ application: UIApplication) {
  58. // 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.
  59. }
  60.  
  61. func applicationWillTerminate(_ application: UIApplication) {
  62. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  63. // Saves changes in the application's managed object context before the application terminates.
  64.  
  65. self.saveContext()
  66. }
  67.  
  68. func registerForPushNotifications() {
  69. UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
  70. (granted, error) in
  71. print("Permission granted: \(granted)")
  72.  
  73. guard granted else { return }
  74. self.getNotificationSettings()
  75. }
  76. }
  77.  
  78. func getNotificationSettings() {
  79. UNUserNotificationCenter.current().getNotificationSettings { (settings) in
  80. print("Notification settings: \(settings)")
  81. guard settings.authorizationStatus == .authorized else { return }
  82.  
  83. DispatchQueue.main.async {
  84. UIApplication.shared.registerForRemoteNotifications()
  85. }
  86.  
  87. }
  88. }
  89.  
  90. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  91. let tokenParts = deviceToken.map { data -> String in
  92. return String(format: "%02.2hhx", data)
  93. }
  94.  
  95. let token = tokenParts.joined()
  96. print("Device Token: \(token)")
  97. }
  98.  
  99. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  100. print("Failed to register: \(error)")
  101. }
  102.  
  103. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  104. // Print message ID.
  105. // if let messageID = userInfo[gcmMessageIDKey] {
  106. // print("Message ID: \(messageID)")
  107. // }
  108.  
  109. // Print full message.
  110. print(userInfo)
  111.  
  112. completionHandler(UIBackgroundFetchResult.newData)
  113. }
  114.  
  115. // MARK: - Core Data stack
  116.  
  117. static var viewContext : NSManagedObjectContext {
  118. return persistentContainer.viewContext
  119. }
  120.  
  121. static var persistentContainer: NSPersistentContainer = {
  122. /*
  123. The persistent container for the application. This implementation
  124. creates and returns a container, having loaded the store for the
  125. application to it. This property is optional since there are legitimate
  126. error conditions that could cause the creation of the store to fail.
  127. */
  128. let container = NSPersistentContainer(name: "Astronomia")
  129. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  130. if let error = error as NSError? {
  131. // Replace this implementation with code to handle the error appropriately.
  132. // 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.
  133.  
  134. /*
  135. Typical reasons for an error here include:
  136. * The parent directory does not exist, cannot be created, or disallows writing.
  137. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  138. * The device is out of space.
  139. * The store could not be migrated to the current model version.
  140. Check the error message to determine what the actual problem was.
  141. */
  142. fatalError("Unresolved error \(error), \(error.userInfo)")
  143. }
  144. })
  145. return container
  146. }()
  147.  
  148. // MARK: - Core Data Saving support
  149.  
  150. func saveContext () {
  151. let context = AppDelegate.viewContext
  152. if context.hasChanges {
  153. do {
  154. try context.save()
  155. } catch {
  156. // Replace this implementation with code to handle the error appropriately.
  157. // 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.
  158. let nserror = error as NSError
  159. fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  160. }
  161. }
  162. }
  163. }
Add Comment
Please, Sign In to add comment