Advertisement
Guest User

Untitled

a guest
May 7th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.17 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  Milkywaisblueyes
  4. //
  5. //  Created by Pol Marnette on 22/04/17.
  6. //  Copyright © 2017 Pol Marnette. All rights reserved.
  7. //
  8.  
  9. //import UserNotifications
  10. import UIKit
  11. import CoreData
  12.  
  13. import UserNotifications
  14. import Firebase
  15. import FirebaseInstanceID
  16. import FirebaseMessaging
  17.  
  18. @UIApplicationMain
  19. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {
  20.    
  21.     var window: UIWindow?
  22.    
  23.    
  24.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  25.         if #available(iOS 10.0, *) {
  26.             // For iOS 10 display notification (sent via APNS)
  27.             UNUserNotificationCenter.current().delegate = self
  28.             let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  29.             UNUserNotificationCenter.current().requestAuthorization(
  30.                 options: authOptions,
  31.                 completionHandler: {_, _ in })
  32.             // For iOS 10 data message (sent via FCM
  33.             FIRMessaging.messaging().remoteMessageDelegate = self
  34.         } else {
  35.             let settings: UIUserNotificationSettings =
  36.                 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  37.             application.registerUserNotificationSettings(settings)
  38.         }
  39.        
  40.         application.registerForRemoteNotifications()
  41.        
  42.         FIRApp.configure()
  43.        
  44.         return true
  45.     }
  46.    
  47.    
  48.     // The callback to handle data message received via FCM for devices running iOS 10 or above.
  49.     func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
  50.         print(remoteMessage.appData)
  51.     }
  52.  
  53.    
  54.     func applicationWillResignActive(_ application: UIApplication) {
  55.         // 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.
  56.         // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  57.     }
  58.    
  59.     func applicationDidEnterBackground(_ application: UIApplication) {
  60.         // 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.
  61.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  62.     }
  63.    
  64.     func applicationWillEnterForeground(_ application: UIApplication) {
  65.         // 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.
  66.        
  67.     }
  68.    
  69.  
  70.    
  71.     func applicationDidBecomeActive(_ application: UIApplication) {
  72.         // 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.
  73.     }
  74.    
  75.     func applicationWillTerminate(_ application: UIApplication) {
  76.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  77.         self.saveContext()
  78.     }
  79.    
  80.     func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
  81.         print("test")
  82.         if let tabVc = self.window?.rootViewController as? UITabBarController{
  83.             if (shortcutItem.type == "com.Polapp.Milkywaysblueyes.fav"){
  84.                 tabVc.selectedIndex = 1
  85.             }
  86.         }
  87.     }
  88.    
  89.     // MARK: - Core Data stack
  90.    
  91.     lazy var persistentContainer: NSPersistentContainer = {
  92.         /*
  93.          The persistent container for the application. This implementation
  94.          creates and returns a container, having loaded the store for the
  95.          application to it. This property is optional since there are legitimate
  96.          error conditions that could cause the creation of the store to fail.
  97.          */
  98.         let container = NSPersistentContainer(name: "Model")
  99.         container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  100.             if let error = error as NSError? {
  101.                 // Replace this implementation with code to handle the error appropriately.
  102.                 // 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.
  103.                
  104.                 /*
  105.                  Typical reasons for an error here include:
  106.                  * The parent directory does not exist, cannot be created, or disallows writing.
  107.                  * The persistent store is not accessible, due to permissions or data protection when the device is locked.
  108.                  * The device is out of space.
  109.                  * The store could not be migrated to the current model version.
  110.                  Check the error message to determine what the actual problem was.
  111.                  */
  112.                 fatalError("Unresolved error \(error), \(error.userInfo)")
  113.             }
  114.         })
  115.         return container
  116.     }()
  117.    
  118.     // MARK: - Core Data Saving support
  119.    
  120.     func saveContext () {
  121.         let context = persistentContainer.viewContext
  122.         if context.hasChanges {
  123.             do {
  124.                 try context.save()
  125.             } catch {
  126.                 // Replace this implementation with code to handle the error appropriately.
  127.                 // 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.
  128.                 let nserror = error as NSError
  129.                 fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  130.             }
  131.         }
  132.     }
  133.    
  134.    
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement