Created
July 12, 2018 03:50
-
-
Save sweeneyapps/ff8ae3761db1761b4098fb6ffd200964 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// | |
// | |
// Created by Paul Sweeney Jr on 10/21/17. | |
// | |
// | |
import UIKit | |
import AWSCore | |
import AWSCognito | |
import AWSCognitoIdentityProvider | |
import AVFoundation | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
var signInViewController: SignInViewController? | |
var mfaViewController: MFAViewController? | |
var navigationController: UINavigationController? | |
var storyboard: UIStoryboard? | |
var rememberDeviceCompletionSource: AWSTaskCompletionSource<NSNumber>? | |
var bgTask: UIBackgroundTaskIdentifier = 0 | |
let app: UIApplication = UIApplication.shared | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
print("application") | |
// Warn user if configuration not updated | |
// if (CognitoIdentityUserPoolId == "YOUR_USER_POOL_ID") { | |
// let alertController = UIAlertController(title: "Invalid Configuration", | |
// message: "Please configure user pool constants in Constants.swift file.", | |
// preferredStyle: .alert) | |
// let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil) | |
// alertController.addAction(okAction) | |
// | |
// self.window?.rootViewController!.present(alertController, animated: true, completion: nil) | |
// } | |
// setup service configuration | |
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil) | |
AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration | |
// create pool configuration | |
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, | |
clientSecret: CognitoIdentityUserPoolAppClientSecret, | |
poolId: CognitoIdentityUserPoolId) | |
// initialize user pool client | |
AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey) | |
// fetch the user pool client we initialized in above step | |
let pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey) | |
pool.delegate = self | |
self.storyboard = UIStoryboard(name: "Main", bundle: nil) | |
// Override point for customization after application launch. | |
return true | |
} | |
func applicationWillResignActive(_ application: UIApplication) { | |
print("applicationWillResignActive") | |
// 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 applicationDidEnterBackground(_ application: UIApplication) { | |
print("applicationDidEnterBackground") | |
// 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. | |
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. | |
} | |
func applicationWillEnterForeground(_ application: UIApplication) { | |
print("applicationWillEnterForeground") | |
// 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. | |
} | |
func applicationDidBecomeActive(_ application: UIApplication) { | |
print("applicationDidBecomeActive") | |
// 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. | |
} | |
func applicationWillTerminate(_ application: UIApplication) { | |
print("applicationWillTerminate") | |
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. | |
} | |
} | |
// MARK:- AWSCognitoIdentityInteractiveAuthenticationDelegate protocol delegate | |
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate { | |
func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication { | |
print("startPasswordAuthentication") | |
self.navigationController = nil | |
self.signInViewController = nil | |
if (self.navigationController == nil) { | |
print("1") | |
self.navigationController = self.storyboard?.instantiateViewController(withIdentifier: "signinController") as? UINavigationController | |
} | |
if (self.signInViewController == nil) { | |
print("2") | |
self.signInViewController = self.navigationController?.viewControllers[0] as? SignInViewController | |
} | |
DispatchQueue.main.async { | |
print("3") | |
print("\(self.navigationController!.isViewLoaded)") | |
self.navigationController!.popToRootViewController(animated: false) | |
if (!self.navigationController!.isViewLoaded | |
/* && self.navigationController!.view.window != nil */ ){ | |
print("4") | |
self.window?.rootViewController?.present(self.navigationController!, | |
animated: true, | |
completion: nil) | |
} | |
} | |
print("5") | |
return self.signInViewController! | |
} | |
func startMultiFactorAuthentication() -> AWSCognitoIdentityMultiFactorAuthentication { | |
print("startMultiFactorAuth") | |
if (self.mfaViewController == nil) { | |
self.mfaViewController = MFAViewController() | |
self.mfaViewController?.modalPresentationStyle = .popover | |
} | |
DispatchQueue.main.async { | |
if (!self.mfaViewController!.isViewLoaded | |
|| self.mfaViewController!.view.window == nil) { | |
//display mfa as popover on current view controller | |
let viewController = self.window?.rootViewController! | |
viewController?.present(self.mfaViewController!, | |
animated: true, | |
completion: nil) | |
// configure popover vc | |
let presentationController = self.mfaViewController!.popoverPresentationController | |
presentationController?.permittedArrowDirections = UIPopoverArrowDirection.left | |
presentationController?.sourceView = viewController!.view | |
presentationController?.sourceRect = viewController!.view.bounds | |
} | |
} | |
return self.mfaViewController! | |
} | |
func startRememberDevice() -> AWSCognitoIdentityRememberDevice { | |
print("startRemeberDevice") | |
return self | |
} | |
} | |
// MARK:- AWSCognitoIdentityRememberDevice protocol delegate | |
extension AppDelegate: AWSCognitoIdentityRememberDevice { | |
func getRememberDevice(_ rememberDeviceCompletionSource: AWSTaskCompletionSource<NSNumber>) { | |
print("getRmemeberDevice") | |
self.rememberDeviceCompletionSource = rememberDeviceCompletionSource | |
DispatchQueue.main.async { | |
// dismiss the view controller being present before asking to remember device | |
self.window?.rootViewController!.presentedViewController?.dismiss(animated: true, completion: nil) | |
let alertController = UIAlertController(title: "Remember Device", | |
message: "Do you want to remember this device?.", | |
preferredStyle: .actionSheet) | |
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: { (action) in | |
self.rememberDeviceCompletionSource?.set(result: true) | |
}) | |
let noAction = UIAlertAction(title: "No", style: .default, handler: { (action) in | |
self.rememberDeviceCompletionSource?.set(result: false) | |
}) | |
alertController.addAction(yesAction) | |
alertController.addAction(noAction) | |
self.window?.rootViewController?.present(alertController, animated: true, completion: nil) | |
} | |
} | |
func didCompleteStepWithError(_ error: Error?) { | |
print("didCompleteStepWithErro") | |
DispatchQueue.main.async { | |
if let error = error as NSError? { | |
let alertController = UIAlertController(title: error.userInfo["__type"] as? String, | |
message: error.userInfo["message"] as? String, | |
preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "ok", style: .default, handler: nil) | |
alertController.addAction(okAction) | |
DispatchQueue.main.async { | |
self.window?.rootViewController?.present(alertController, animated: true, completion: nil) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment