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

标签 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

但他还没有得到答案。

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

最佳答案

适用于 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
}

关于swift - 如何在 iOS (Swift) 中对 AWS Appsync 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51476502/

相关文章:

ios - Xcode 的 "Target Membership"是否与 Bundle 相关?

amazon-web-services - Heroku SSL Cloudfront 缺少 key 对 ID 403 错误

ios - 创建 GIDGoogleUser 的模拟实例

ios - 如何根据 Swift 中的条件返回不同枚举的数组?

arrays - Swift - 虽然循环不起作用

swift - 无法滚动到可缩放图像中的每个位置

ios - 快速检测 Sprite 节点是否存在

iphone - 如何让 UIWebView 加载网页,但 URL 在 UILabel 上?

amazon-web-services - Elasticsearch渗透在AWS EC2上非常缓慢

amazon-web-services - AWS S3 在区域之间迁移 50 TB 数据