3

I am trying to connect to AWS Cognito but it seems that there are several way to do that and I want to go for the right one. I want to add a signup/signin feature to my app using AWS Cognito User Pool.

When I look at the sample provided 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 build the 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 fairly understand the piece coming from sdk sample (except from the fact that I do not need to provide a credentialsProvider to the AWSServiceConfiguration). But I am completely lost with the MobileHub one. It seems that ASW has implement a new logic in MobileHub. Can someone explain this logic ?

user3239711
  • 639
  • 1
  • 7
  • 24
  • Do you have a specific question about what the MobileHub SDK is doing? In sweeping terms, they are both configuring a user pools client. – perpil Dec 08 '17 at 01:41

0 Answers0