Connecting to AWS Cognito from iOS

I'm trying to connect to AWS Cognito but it seems like there are multiple ways to do it and I want to go for the right one. I want to add signup / signin functionality to my application using AWS Cognito User Pool.

When I look at the sample that comes with the AWS iOS Sdk, I have to add this to the AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // setup service configuration
        let serviceConfiguration = AWSServiceConfiguration(region: CognitoIdentityUserPoolRegion, credentialsProvider: nil)

        // 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

        return true
    }

      

When I create a sample app using MobileHub I have this code instead

func didFinishLaunching(_ application: UIApplication, withOptions launchOptions: [AnyHashable: Any]?) -> Bool {
    print("didFinishLaunching:")

    // Register the sign in provider instances with their unique identifier
    AWSSignInManager.sharedInstance().register(signInProvider: AWSCognitoUserPoolsSignInProvider.sharedInstance())
    AWSIdentityProfileManager.sharedInstance().register(UserPoolsIdentityProfile.sharedInstance(), forProviderKey: AWSCognitoUserPoolsSignInProvider.sharedInstance().identityProviderName)

    // set up Cloud Logic API invocation clients
    setupCloudLogicAPI()


    let didFinishLaunching: Bool = AWSSignInManager.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

    if (!isInitialized) {
        AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
            print("Result: \(result) AuthState: \(authState) \n Error:\(error)")
        }) // If you get an EXC_BAD_ACCESS here in iOS Simulator, then do Simulator -> "Reset Content and Settings..."
           // This will clear bad auth tokens stored by other apps with the same bundle ID.
        isInitialized = true
    }

    return didFinishLaunching
}


func setupCloudLogicAPI() {
    let serviceConfiguration = AWSServiceConfiguration(region: AWSCloudLogicDefaultRegion, credentialsProvider: AWSIdentityManager.default().credentialsProvider)
    AWSAPI_DX0LXS4OH9_MoujibApiMobileHubClient.register(with: serviceConfiguration!, forKey: AWSCloudLogicDefaultConfigurationKey)
}

      

I'm pretty good at the part derived from the sdk example (except for the fact that I don't need to provide an accountProvider for the AWSServiceConfiguration). But I am completely lost with MobileHub. It looks like ASW is implementing new logic in MobileHub. Can anyone explain this logic?

+3


source to share





All Articles