How to authenicate AWS Appsync in iOS (Swift)
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
}
Appsync return 401 errors when connecting with cognito
Ok the issue was: If you're using the code above, you need to set your appsync to authenticate via IAM (not Cognito). This will also require changes to your resolvers since the parameters passed to the identity object are different for IAM vs Cognito.
This is confusing because you're using Cognito (both a user pool and a federated identity user pool), but do NOT chose Cognito.
AWS Appsync - unable to find User Pool Client ID
The User Pool Client ID is available from the Amazon Cognito User Pools console in the App Clients section.
You should create an App Client if it doesn't already exist. Make sure to uncheck the "Generate client secret" box.
Related Topics
Problem with Swiftui and Foreach on Xcode Playground
Lldb for Swift: Access Computed Property or Perform Function Call in Type Summary Python Script
How to Make Alamofire Post Request with "Form-Data" Parameters
Swiftui Change on Multilevel Children Published Object Change
Realm Write Transaction Failing, Despite Being in Transaction
How to Make UItabbar Image Rounded in Swift Programmatically
Swiftui New App Lifecycle How to Connect The Facebook Sdk
Swift Method Parameters in Documentation
(Key: Anyobject, Value: Anyobject)' Does Not Have a Member Named 'subscript'
Biometric Authentication Evaluation with Swiftui
Why Swift Disallows Weak Reference for Non-Optional Type
How to Create Nested Dictionary Elements in Swift
Alamofire Request Not Working in Swift 4 Project
How to Calculate The Camera Position from Vuforia Gl Matrix
Can't Override UItableviewdatasource and UItableviewdelegate
Swift 4: How to Asynchronously Use Urlsessiondatatask But Have The Requests Be in a Timed Queue
Urlcomponents Queryitems Losing Percent Encoding When Mutated