如何在 iOS (Swift) 中对 AWS Appsync 进行身份验证

问题描述 投票:0回答:2

这里是初级开发人员。

我正在尝试让 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>"

});

我在这里发现了类似的问题: 无法使用 swift SDK 对 aws appsync 的用户进行身份验证

但他还没有得到答案。

我用谷歌搜索了很多,但似乎找不到解决方案。 你们中的一位善良的程序员灵魂能给我指出正确的方向吗?

swift xcode amazon-web-services authentication aws-appsync
2个回答
2
投票

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

0
投票

如果我想重新初始化 appSyncClient 的实例怎么办?因为我有多个环境,并且我已经传递了自定义配置而不是默认配置(这样它就可以通过捆绑访问从 json 文件获取配置)

© www.soinside.com 2019 - 2024. All rights reserved.