如何在iOS(Swift)中认证AWS Appsync

时间:2018-07-23 10:20:56

标签: swift xcode amazon-web-services authentication aws-appsync

这里是初级开发人员。

我正在尝试使AWS Appsync在我当前正在构建的iOS应用程序中工作,但是我无法使身份验证工作。

我想快速复制此Javascript代码,以进行身份​​验证。

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

});

我在这里发现了类似的问题: cannot authenticate user for aws appsync with swift SDK

但是他没有得到答案。

我在Google上搜索了很多,但似乎找不到解决方法。 你们其中一个程序员灵魂能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

适用于iOS的AWS开发工具包-AppSync解决了您的用例。您可以在此处查看文档:{​​{3}}。您可以在此处查看源代码:https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-ios.html。有一个入门应用程序可以帮助您快速入门:https://github.com/awslabs/aws-mobile-appsync-sdk-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
}