IBM Cloud App ID iOS Swift SDK
要求
- Xcode 8.1 或更高版
- CocoaPods 1.1.0 或更高版
- MacOS 10.11.5 或更高版
- iOS 9 或更高版
安装 SDK
-
将 'BluemixAppID' 依赖关系添加到您的 Podfile 中,例如
target <yourTarget> do use_frameworks! pod 'BluemixAppID' end
-
从终端,运行以下命令:
pod install --repo-update
初始化 App ID 客户端 SDK
-
打开您的 Xcode 项目并启用 Keychain 共享(在项目设置 > 功能 > 键链共享中操作)
-
在项目设置 > 信息 > URL 类型下,添加 $(PRODUCT_BUNDLE_IDENTIFIER) 作为 URL Scheme
-
将以下导入添加到您的 AppDelegate.swift 文件中
import BluemixAppID
-
通过向 initialize 方法传递 tenantId 和 region 参数来初始化客户端 SDK。将初始化代码放在 Swift 应用中的 AppDelegate 的 application:didFinishLaunchingWithOptions: 方法中是一个常见但非强制的做法。
AppID.sharedInstance.initialize(tenantId: <tenantId>, bluemixRegion: AppID.REGION_UK)
- 将 "tenantId" 替换为 App ID 服务 tenantId。
- 将 AppID.REGION_UK 替换为您的 App ID 区域(AppID.REGION_US_SOUTH, AppID.REGION_SYDNEY)。
-
将以下代码添加到您的 AppDelegate 文件中
func application(_ application: UIApplication, open url: URL, options :[UIApplicationOpenURLOptionsKey : Any]) -> Bool { return AppID.sharedInstance.application(application, open: url, options: options) }
使用登录小部件
初始化 App ID 客户端 SDK 后,您可以通过启动登录小部件来开始用户认证。
- 将以下导入添加到您想要与登录小部件一起使用的文件中
import BluemixAppID
- 将以下代码添加到同一文件中
class delegate : AuthorizationDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
//User authenticated
}
public func onAuthorizationCanceled() {
//Authentication canceled by the user
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.loginWidget?.launch(delegate: delegate())
注意:
- 登录小部件的默认配置使用 Facebook 和 Google 作为身份验证选项。如果您只配置了其中之一,则登录小部件将 无法 启动,并且用户将被重定向到配置的标识提供者身份验证屏幕。
- 当使用 Cloud Directory 时,如果已将“电子邮件验证”配置为不允许用户在没有电子邮件验证的情况下登录,那么 "AuthorizationListener" 的 "onAuthorizationSuccess" 将在没有令牌的情况下被调用。
使用 iOS Swift SDK 管理云目录
使用资源所有者密码登录
通过提供最终用户的用户名和密码,可以获取访问令牌和 id 令牌。
class delegate : TokenResponseDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
//User authenticated
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.signinWithResourceOwnerPassword(username: username, password: password, delegate: delegate())
{: codeblock}
注册
请确保在 Cloud Directory 的设置中将 允许用户注册和重置密码 设置为 开启。
使用 LoginWidget 类启动注册流程。
class delegate : AuthorizationDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
if accessToken == nil && identityToken == nil {
//email verification is required
return
}
//User authenticated
}
public func onAuthorizationCanceled() {
//Sign up canceled by the user
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.loginWidget?.launchSignUp(delegate: delegate())
忘记密码
确保在云目录的设置中,将“允许用户注册和重置密码”和“忘记密码邮件”设置为“开”。
使用LoginWidget类开始忘记密码流程。
class delegate : AuthorizationDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
//forgot password finished, in this case accessToken and identityToken will be null.
}
public func onAuthorizationCanceled() {
//forgot password canceled by the user
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.loginWidget?.launchForgotPassword(delegate: delegate())
更改详情
请确保在 Cloud Directory 的设置中将 允许用户注册和重置密码 设置为 开启。
使用LoginWidget类开始更改详情流程。此API只能在用户通过云目录身份提供者登录时使用。
class delegate : AuthorizationDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
//User authenticated, and fresh tokens received
}
public func onAuthorizationCanceled() {
//changed details canceled by the user
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.loginWidget?.launchChangeDetails(delegate: delegate())
更改密码
请确保在 Cloud Directory 的设置中将 允许用户注册和重置密码 设置为 开启。
使用LoginWidget类开始更改密码流程。此API只能在用户通过云目录身份提供者登录时使用。
class delegate : AuthorizationDelegate {
public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
//User authenticated, and fresh tokens received
}
public func onAuthorizationCanceled() {
//change password canceled by the user
}
public func onAuthorizationFailure(error: AuthorizationError) {
//Exception occurred
}
}
AppID.sharedInstance.loginWidget?.launchChangePassword(delegate: delegate())
调用受保护资源
将以下导入添加到您希望调用受保护资源请求的文件中
import BMSCore
import BluemixAppID
然后添加以下代码
BMSClient.sharedInstance.initialize(bluemixRegion: AppID.REGION_UK)
BMSClient.sharedInstance.authorizationManager = AppIDAuthorizationManager(appid:AppID.sharedInstance)
var request:Request = Request(url: "<your protected resource url>")
request.send(completionHandler: {(response:Response?, error:Error?) in
//code handling the response here
})
许可
本软件包包含根据Apache License,版本2.0授权的代码。您可以在https://apache.ac.cn/licenses/LICENSE-2.0获取许可副本,也可以在此软件包的LICENSE文件中查看许可。