123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- //
- // AppDelegate.swift
- // EncuestaMarle
- //
- // Created by Tatiana Castro on 4/13/19.
- // Copyright © 2019 Marle. All rights reserved.
- //
-
- import UIKit
- import CoreData
- import AWSSNS
- import UserNotifications
- import Firebase
- import ResearchKit
-
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
-
- let SNSPlatformApplicationArn = "arn:aws:sns:us-east-1:227586183436:app/APNS_SANDBOX/MARLE_iOS"
-
- var containerViewController: ResearchContainerViewController? {
- return window?.rootViewController as? ResearchContainerViewController
- }
-
- var window: UIWindow?
-
- func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
- let standardDefaults = UserDefaults.standard
- if standardDefaults.object(forKey: "EncuestaMarleFirstRun") == nil {
- standardDefaults.setValue("EncuestaMarleFirstRun", forKey: "EncuestaMarleFirstRun")
- }
- // UserDefaults.standard.set(false, forKey: "registered")
- let pageControlAppearance = UIPageControl.appearance()
- pageControlAppearance.pageIndicatorTintColor = UIColor.lightGray
- pageControlAppearance.currentPageIndicatorTintColor = UIColor.black
-
- return true
- }
-
- func applicationDidEnterBackground(_ application: UIApplication) {
- UserDefaults.standard.synchronize()
- if ORKPasscodeViewController.isPasscodeStoredInKeychain() {
- // Hide content so it doesn't appear in the app switcher.
- containerViewController?.contentHidden = true
- }
- }
-
- func applicationWillEnterForeground(_ application: UIApplication) {
- }
-
-
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
-
- let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1, identityPoolId:"us-east-1:b8e84ae3-42e8-4655-a70e-1dcd8d053db5")
-
- let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
-
- AWSServiceManager.default().defaultServiceConfiguration = configuration
-
- registerForPushNotifications(application: application)
-
- // let registered = UserDefaults.standard.bool(forKey: "registered")
- //
- // print("User is registered❓")
- // print(registered)
- //
- // if registered {
- //
- // let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
- //
- // let protectedPage = mainStoryBoard.instantiateViewController(withIdentifier: "EncuestaViewController") as! EncuestaViewController
- // window!.rootViewController = protectedPage
- // window!.makeKeyAndVisible()
- // }
-
- return true
- }
-
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- token = ""
- for i in 0..<deviceToken.count {
- token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
- }
- print(token)
- UserDefaults.standard.set(token, forKey: "deviceTokenForSNS")
- let sns = AWSSNS.default()
- let request = AWSSNSCreatePlatformEndpointInput()
- request?.token = token
- request?.platformApplicationArn = SNSPlatformApplicationArn
- sns.createPlatformEndpoint(request!).continueWith(executor: AWSExecutor.mainThread(), block: { (task: AWSTask!) -> AnyObject? in
- if task.error != nil {
- print("Error: \(String(describing: task.error))")
- } else {
- let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponse
- if let endpointArnForSNS = createEndpointResponse.endpointArn {
- print("endpointArn: \(endpointArnForSNS)")
- UserDefaults.standard.set(endpointArnForSNS, forKey: "endpointArnForSNS")
- UserDefaults.standard.set(token, forKey: "user_token")
- }
- }
- return nil
- })
- }
-
- func registerForPushNotifications(application: UIApplication) {
- if #available(iOS 10.0, *) {
- UNUserNotificationCenter.current().delegate = self
- UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
- if (granted)
- {
- UIApplication.shared.registerForRemoteNotifications()
- }
- else{
- }
- })
- } else {
- let settings = UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound], categories: nil)
- application.registerUserNotificationSettings(settings)
- application.registerForRemoteNotifications()
- }
- }
-
- @available(iOS 10.0, *)
-
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- print("User Info = ",notification.request.content.userInfo)
- completionHandler([.alert, .badge, .sound])
- }
-
- @available(iOS 10.0, *)
-
- func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
-
- print("User Info = ",response.notification.request.content.userInfo)
- completionHandler()
- }
-
-
- func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
- print(error.localizedDescription)
- }
-
- func applicationWillResignActive(_ application: UIApplication) {
- // 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.
- // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
- }
-
-
- func applicationDidBecomeActive(_ application: UIApplication) {
- // 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.
- getJsonFromUrl()
- }
-
- func applicationWillTerminate(_ application: UIApplication) {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- // Saves changes in the application's managed object context before the application terminates.
- self.saveContext()
- }
-
- // MARK: - Core Data stack
-
- lazy var persistentContainer: NSPersistentContainer = {
- /*
- The persistent container for the application. This implementation
- creates and returns a container, having loaded 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.
- */
- let container = NSPersistentContainer(name: "EncuestaMarle")
- container.loadPersistentStores(completionHandler: { (storeDescription, error) in
- if let error = error as NSError? {
- // Replace this implementation with code to handle the error appropriately.
- // 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.
-
- /*
- Typical reasons for an error here include:
- * The parent directory does not exist, cannot be created, or disallows writing.
- * The persistent store is not accessible, due to permissions or data protection when the device is locked.
- * The device is out of space.
- * The store could not be migrated to the current model version.
- Check the error message to determine what the actual problem was.
- */
- fatalError("Unresolved error \(error), \(error.userInfo)")
- }
- })
- return container
- }()
-
-
-
- // MARK: - Core Data Saving support
-
- func saveContext () {
- let context = persistentContainer.viewContext
- if context.hasChanges {
- do {
- try context.save()
- } catch {
- // Replace this implementation with code to handle the error appropriately.
- // 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.
- let nserror = error as NSError
- fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
- }
- }
- }
-
- }
-
- extension AppDelegate: ORKPasscodeDelegate {
- func passcodeViewControllerDidFinish(withSuccess viewController: UIViewController) {
- containerViewController?.contentHidden = false
- viewController.dismiss(animated: true, completion: nil)
- }
-
- func passcodeViewControllerDidFailAuthentication(_ viewController: UIViewController) {
- }
- }
-
-
|