Hamburglar
Hamburglar

Reputation: 550

Generating AWS creditials through Cognito Using Swift iOS App

Trying to follow the example set on the amazon-cognito site with regards to obtaining AWS credentials from a facebook login on iOS. I am trying to translate the Objective C into swift and am running into an issue when setting the logins of the credential provider.

(http://docs.aws.amazon.com/mobile/sdkforios/developerguide/cognito-auth.html) under "Use Facebook"

Here in my viewController I try:

    let token = FBSession.activeSession().accessTokenData.accessToken

    let credentialsProvider : AWSCredentialsProvider = AWSServiceManager.defaultServiceManager().defaultServiceConfiguration.credentialsProvider

    credentialsProvider.logins = NSDictionary(dictionary: [AWSCognitoLoginProviderKey.Facebook: token])

but on the third line xCode states AWSCredentialsProvider does not have a field "logins" though the tutorial makes it seem as such.

Thanks for your help! I assume I am incorrectly grabbing the credentialsProvider.

Upvotes: 1

Views: 2070

Answers (2)

John Nimis
John Nimis

Reputation: 606

I think the way to do this is to write a custom provider: a class that implements the AWSIdentityProviderManager protocol: http://docs.aws.amazon.com/AWSiOSSDK/latest/Protocols/AWSIdentityProviderManager.html

This protocol has basically one method, "logins" which you use to return your NSDictionary of credentials. So instead of this line of code:

let credentialsProvider : AWSCredentialsProvider = AWSServiceManager.defaultServiceManager().defaultServiceConfiguration.credentialsProvider

you would design a custom class (most of this is straight from the AWS docs):

class MyIdentityProvider: NSObject, AWSIdentityProviderManager {

func logins() -> AWSTask<NSDictionary> {
  if let token = FBSDKAccessToken.current() {
    return AWSTask(result: [AWSIdentityProviderFacebook:token])
  }  
  return AWSTask(error:NSError(domain: "Facebook Login", code: -1 , userInfo: ["Facebook" : "No current Facebook access token"]))
}

then you instantiate your AWSService manager using your custom class:

let identityProvider = MyIdentityProvider()
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .usEast1,
     identityPoolId: "<your poolId>",
     identityProviderManager: identityProvider)
let configuration = AWSServiceConfiguration(region:.usEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

See an example in this SO answer (to a related question): [iOS][AWS Cognito] 'logins' is deprecated: Use "AWSIdentityProviderManager"

Upvotes: 0

Yosuke
Yosuke

Reputation: 3759

The AWSCredentialsProvider protocol does not define the login property. You need to cast the credentials provider to AWSCognitoCredentialsProvider in order to access it. Another option is to retain a reference to an instance of AWSCognitoCredentialsProvider for yourself to update the property.

Upvotes: 1

Related Questions