文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 SDK for Swift 的 Amazon Cognito Identity 範例
下列程式碼範例示範如何使用 AWS SDK for Swift 搭配 Amazon Cognito Identity 來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
下列程式碼範例示範如何使用 CreateIdentityPool
。
- SDK for Swift
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import AWSCognitoIdentity /// Create a new identity pool and return its ID. /// /// - Parameters: /// - name: The name to give the new identity pool. /// /// - Returns: A string containing the newly created pool's ID, or `nil` /// if an error occurred. /// func createIdentityPool(name: String) async throws -> String? { do { let cognitoInputCall = CreateIdentityPoolInput(developerProviderName: "com.exampleco.CognitoIdentityDemo", identityPoolName: name) let result = try await cognitoIdentityClient.createIdentityPool(input: cognitoInputCall) guard let poolId = result.identityPoolId else { return nil } return poolId } catch { print("ERROR: createIdentityPool:", dump(error)) throw error } }
-
如需詳細資訊,請參閱 AWS SDK for Swift 開發人員指南。
-
如需 API 詳細資訊,請參閱 CreateIdentityPool
AWS for Swift SDK 參考中的 API。
-
下列程式碼範例示範如何使用 DeleteIdentityPool
。
- SDK for Swift
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import AWSCognitoIdentity /// Delete the specified identity pool. /// /// - Parameters: /// - id: The ID of the identity pool to delete. /// func deleteIdentityPool(id: String) async throws { do { let input = DeleteIdentityPoolInput( identityPoolId: id ) _ = try await cognitoIdentityClient.deleteIdentityPool(input: input) } catch { print("ERROR: deleteIdentityPool:", dump(error)) throw error } }
-
如需詳細資訊,請參閱 AWS SDK for Swift 開發人員指南。
-
如需 API 詳細資訊,請參閱 DeleteIdentityPool
AWS for Swift SDK 參考中的 API。
-
下列程式碼範例示範如何使用 ListIdentityPools
。
- SDK for Swift
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import AWSCognitoIdentity /// Return the ID of the identity pool with the specified name. /// /// - Parameters: /// - name: The name of the identity pool whose ID should be returned. /// /// - Returns: A string containing the ID of the specified identity pool /// or `nil` on error or if not found. /// func getIdentityPoolID(name: String) async throws -> String? { let listPoolsInput = ListIdentityPoolsInput(maxResults: 25) // Use "Paginated" to get all the objects. // This lets the SDK handle the 'nextToken' field in "ListIdentityPoolsOutput". let pages = cognitoIdentityClient.listIdentityPoolsPaginated(input: listPoolsInput) do { for try await page in pages { guard let identityPools = page.identityPools else { print("ERROR: listIdentityPoolsPaginated returned nil contents.") continue } /// 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. for pool in identityPools { if pool.identityPoolName == name { return pool.identityPoolId! } } } } catch { print("ERROR: getIdentityPoolID:", dump(error)) throw error } return nil }
取得現有身分集區的 ID 或建立它 (如果尚不存在)。
import AWSCognitoIdentity /// Return the ID of the identity pool with the specified name. /// /// - Parameters: /// - name: The name of the identity pool whose ID should be returned /// /// - Returns: A string containing the ID of the specified identity pool. /// Returns `nil` if there's an error or if the pool isn't found. /// public func getOrCreateIdentityPoolID(name: String) async throws -> String? { // See if the pool already exists. If it doesn't, create it. do { guard let poolId = try await getIdentityPoolID(name: name) else { return try await createIdentityPool(name: name) } return poolId } catch { print("ERROR: getOrCreateIdentityPoolID:", dump(error)) throw error } }
-
如需詳細資訊,請參閱 AWS SDK for Swift 開發人員指南。
-
如需 API 詳細資訊,請參閱 ListIdentityPools
AWS for Swift SDK 參考中的 API。
-