Amazon Cognito Identity examples using SDK for Swift
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Swift with Amazon Cognito Identity.
Actions are code excerpts that show you how to call individual service functions.
Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.
Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to find the Amazon Cognito Identity with the given name, creating it if it's not found.
- SDK for Swift
-
Note This is prerelease documentation for an SDK in preview release. It is subject to change.
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create a new identity pool.
func createIdentityPool(name: String) async throws -> String? { let cognitoInputCall = CreateIdentityPoolInput(developerProviderName: "com.exampleco.CognitoIdentityDemo", identityPoolName: name) do { let result = try await cognitoIdentityClient.createIdentityPool(input: cognitoInputCall) guard let poolId = result.identityPoolId else { return nil } return poolId } catch { print("ERROR: ", dump(error, name: "Error attempting to create the identity pool")) } return nil }
-
For more information, see AWS SDK for Swift developer guide.
-
For API details, see the following topics in AWS SDK for Swift API reference.
-
The following code example shows how to get a list of Amazon Cognito Identity pools.
- SDK for Swift
-
Note This is prerelease documentation for an SDK in preview release. It is subject to change.
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Find the ID of an identity pool given its name.
func getIdentityPoolID(name: String) async throws -> String? { var token: String? = nil // Iterate over the identity pools until a match is found. repeat { /// `token` is a value returned by `ListIdentityPools()` if the returned list /// of identity pools is only a partial list. You use the `token` to tell Cognito that /// you want to continue where you left off previously; specifying `nil` or not providing /// it means "start at the beginning." let listPoolsInput = ListIdentityPoolsInput(maxResults: 25, nextToken: token) /// Read pages of identity pools from Cognito until one is found /// whose name matches the one specified in the `name` parameter. /// Return the matching pool's ID. Each time we ask for the next /// page of identity pools, we pass in the token given by the /// previous page. do { let output = try await cognitoIdentityClient.listIdentityPools(input: listPoolsInput) if let identityPools = output.identityPools { for pool in identityPools { if pool.identityPoolName == name { return pool.identityPoolId! } } } token = output.nextToken } catch { print("ERROR: ", dump(error, name: "Trying to get list of identity pools")) } } while token != nil return nil }
Get the ID of an existing identity pool or create it if it doesn't already exist.
public func getOrCreateIdentityPoolID(name: String) async throws -> String? { // See if the pool already exists do { guard let poolId = try await self.getIdentityPoolID(name: name) else { let poolId = try await self.createIdentityPool(name: name) return poolId } return poolId } catch { print("ERROR: ", dump(error, name: "Trying to get the identity pool ID")) return nil } }
-
For more information, see AWS SDK for Swift developer guide.
-
For API details, see the following topics in AWS SDK for Swift API reference.
-