swiftxcodeamazon-web-servicesauthenticationaws-appsync

How to authenicate AWS Appsync in iOS (Swift)


Junior developer here.

I'm trying to get AWS Appsync to work in an iOS app I'm currently building, but I cant get authentication working.

I want to replicate this Javascript code in swift, for my authenication.

Amplify.configure({

Auth: {

region: "<REGION>",

userPoolId: "<USER-POOL-ID>",

userPoolWebClientId: "<USER-POOL-WEB-CLIENT-ID>"

}

});

const client = new AWSAppSyncClient({

auth: {

jwtToken: async () =>

(await Auth.currentSession()).getIdToken().getJwtToken(),

type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS

},

disableOffline: true,

region: "<REGION>",

url:

"<ENDPOINT-URL>"

});

I have found a similar problem here: cannot authenticate user for aws appsync with swift SDK

But he hasn't gotten an answer.

I have googled plenty but cant seem to find a solution. Could one of you kind programer souls point me in the right direction?


Solution

  • AWS SDK for iOS - AppSync solves your use-case. You can check out the documentation here: https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-ios.html. You can check out the source code here: https://github.com/awslabs/aws-mobile-appsync-sdk-ios. There is a starter app which will help you onboard quickly: https://github.com/aws-samples/aws-mobile-appsync-events-starter-ios.

    import UIKit
    import AWSAppSync
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
       var window: UIWindow?
       var appSyncClient: AWSAppSyncClient?
    
       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
           // Set up Amazon Cognito credentials
           let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityRegion,
                                                                   identityPoolId: CognitoIdentityPoolId)
           // You can choose your database location, accessible by the SDK
           let databaseURL = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent(database_name)
    
           do {
               // Initialize the AWS AppSync configuration
               let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL,
                                                                     serviceRegion: AppSyncRegion,
                                                                     credentialsProvider: credentialsProvider,
                                                                     databaseURL:databaseURL)
               // Initialize the AWS AppSync client
               appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
               // Set id as the cache key for objects
               appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }
           } catch {
               print("Error initializing appsync client. \(error)")
           }
           return true
       }
    
       // ... other intercept methods
    }