iOS 移动身份验证 SDK
这些实用程序帮助您在通过 iOS 应用程序调用 Amazon Location Service API 时进行身份验证。在使用 Amazon Cognito 或 API 密钥作为身份验证方法时,该身份验证 SDK 特别有用。
iOS 移动身份验证 SDK 可在 github 上找到:适用于 iOS 的 Amazon Location Service 移动身份验证 SDK
安装
在 Xcode 项目中安装该 SDK:
转到文件,然后在 XCode 项目中选择添加软件包依赖项。
在搜索栏中键入软件包 URL:https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/
,然后按 Enter 键。 选择
amazon-location-mobile-auth-sdk-ios
软件包并按添加软件包。选择
AmazonLocationiOSAuthSDK
软件包产品并按添加软件包。
身份验证函数
身份验证帮助程序 SDK 具有以下函数:
authHelper.authenticateWithApiKey("My-Amazon-Location-API-Key"): LocationCredentialsProvider
:此函数返回初始化后的LocationCredentialsProvider
以使用 API 密钥。authHelper.authenticateWithCognitoIdentityPool("My-Cognito-Identity-Pool-Id"): LocationCredentialsProvider
:此函数返回初始化后的LocationCredentialsProvider
以使用 Amazon Cognito 身份池。
用法
要使用该移动身份验证 SDK,请将以下语句添加到自己的活动中:
import AmazonLocationiOSAuthSDK import AWSLocationXCF
在创建身份验证帮助程序和位置客户端提供程序实例时,您有两个选项。您可以使用 Amazon Location API 密钥或 Amazon Cognito 创建实例。
-
要使用 Amazon Location API 密钥创建身份验证帮助程序实例,请按如下方式声明帮助程序类:
let authHelper = AuthHelper() let locationCredentialsProvider = authHelper.authenticateWithAPIKey(apiKey: "
My-Amazon-Location-API-Key
", region: "account-region
") -
要使用 Amazon Cognito 创建身份验证帮助程序实例,请按如下方式声明帮助程序类:
let authHelper = AuthHelper() let locationCredentialsProvider = authHelper.authenticateWithCognitoUserPool(identityPoolId: "
My-Amazon-Location-API-Key
", region: "account-region
")
您可以使用位置凭证提供程序创建 Amazon Location 客户端实例,然后调用 Amazon Location Service。以下示例搜索指定纬度和经度附近的位置。
let locationClient = AWSLocation.default() let searchPlaceIndexForPositionRequest = AWSLocationSearchPlaceIndexForPositionRequest()! searchPlaceIndexForPositionRequest.indexName = "My-Place-Index-Name" searchPlaceIndexForPositionRequest.position = [30.405423, -97.718833] let nearbyPlaces = locationClient.searchPlaceIndex(forPosition: searchPlaceIndexForPositionRequest)