如何在 iOS (Swift) 中验证 AWS Appsync
How to authenicate AWS Appsync in iOS (Swift)
这里是初级开发人员。
我正在尝试让 AWS Appsync 在我目前正在构建的 iOS 应用程序中工作,但我无法进行身份验证。
我想在 swift 中复制这个 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 SDK - AppSync 解决了您的用例。您可以在此处查看文档: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 = { [=10=]["id"] }
} catch {
print("Error initializing appsync client. \(error)")
}
return true
}
// ... other intercept methods
}
这里是初级开发人员。
我正在尝试让 AWS Appsync 在我目前正在构建的 iOS 应用程序中工作,但我无法进行身份验证。
我想在 swift 中复制这个 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 SDK - AppSync 解决了您的用例。您可以在此处查看文档: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 = { [=10=]["id"] }
} catch {
print("Error initializing appsync client. \(error)")
}
return true
}
// ... other intercept methods
}