OwnID Gigya-iOS SDK
OwnID Gigya-iOS SDK 集成了基于 Email/Password 的 Gigya 验证,适用于不使用 Screen-Sets 的应用。如果您的应用使用 Gigya Screen-Sets,则应使用 OwnID Gigya-Screen-Sets iOS SDK。有关说明,请参阅 Gigya-Screen-Sets 指南。
OwnID Gigya-iOS SDK 是一个用 Swift 编写的客户端库,通过使用密码学密钥替换传统密码,为您的 iOS 应用提供了无密码登录的替代方案。将 SDK 集成到您的 iOS 应用中向其注册和登录界面添加了 Skip Password 选项。
目录
在开始之前
在将OwnID集成到您的iOS应用程序之前,您必须创建一个OwnID应用程序并将其与您的Gigya项目集成。有关分步说明,请参阅OwnID-Gigya集成基础。
此外,确保您已经将所有操作完成,以将Gigya身份验证添加到您的iOS项目。
添加包依赖项
SDK通过CocoaPods分发。使用CocoaPods将以下包依赖项添加到您的项目中
pod 'ownid-gigya-ios-sdk'
将属性列表文件添加到项目
当应用程序启动时,OwnID SDK会自动从文件系统读取OwnIDConfiguration.plist
,以配置创建的默认实例。至少,此PLIST文件定义了重定向URI和唯一的App ID。创建OwnIDConfiguration.plist
并定义以下必需参数
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OwnIDRedirectionURL</key>
<string>com.myapp.demo://bazco</string>
<key>OwnIDAppID</key>
<string>4tb9nt6iaur0zv</string>
</dict>
</plist>
在哪里
OwnIDAppID
是唯一的App ID,您可以从OwnID控制台获取。OwnIDRedirectionURL
是完整的重定向URL,包括其自定义方案。此URL的自定义方案必须与您在目标中定义的方案匹配。
创建URL类型(自定义URL方案)
您需要打开您的项目并创建一个新的URL类型,以便与在OwnIDConfiguration.plist
中指定的重定向URL相对应。在Xcode中,请转到信息 > URL类型,然后使用URL方案字段指定重定向URL。例如,如果OwnIDRedirectionURL
键的值为com.myapp.demo://bazco
,则可以将com.myapp.demo
复制并粘贴到URL方案字段中。
导入OwnID模块
一旦您添加了OwnID依赖包,您需要导入OwnID模块以便可以使用SDK功能。当您在项目中实现OwnID时,请将以下内容添加到源文件中
import OwnIDGigyaSDK
初始化SDK
必须通过使用configure()
函数正确地初始化OwnID SDK,最好在应用程序的主要入口点(在@main
App
结构中)进行。例如,输入
@main
struct ExampleApp: App {
init() {
OwnID.GigyaSDK.configure()
}
}
如果您没有按照创建OwnIDConfiguration.plist
文件的推荐操作,您需要调用configure
函数时指定参数。有关详细信息,请参阅配置函数的替代语法。
实现注册屏幕
在设计为模型-视图-视图模型(MVVM)的架构模式中,向您的注册屏幕添加跳过密码选项就像添加一个OwnID视图模型和订阅到应用程序的视图模型层一样简单,然后添加OwnID视图到主视图中。就是这样!当用户选择跳过密码时,您的应用程序将等待用户与OwnID Web应用程序交互的事件,然后当用户完成跳过密码过程后调注册用户的函数。
重要:当用户使用OwnID注册时,会生成并设置一个随机密码供该用户的Gigya账号使用。
自定义视图模型
将添加跳过密码UI的OwnID视图绑定到OwnID视图模型的一个实例。在修改您的视图层之前,在您的视图模型层中创建此视图模型的实例,即OwnID.FlowsSDK.RegisterView.ViewModel
。
final class MyRegisterViewModel: ObservableObject {
// MARK: OwnID
let ownIDViewModel = OwnID.GigyaSDK.registrationViewModel(instance: <Your Instance Of Gigya>)
}
创建此OwnID视图模型后,您的视图模型层应监听来自OwnID事件发布者的事件,这使得您的应用程序能够根据用户与OwnID Web应用程序的交互采取操作。只需将以下内容添加到现有的视图模型层中,以订阅 OwnID 事件发布者并响应事件(它可以放置在创建OwnID视图模型实例的代码之后)。
final class MyRegisterViewModel: ObservableObject {
// MARK: OwnID
let ownIDViewModel = OwnID.GigyaSDK.registrationViewModel(instance: <Your Instance Of Gigya>)
init() {
subscribe(to: ownIDViewModel.eventPublisher)
}
func subscribe(to eventsPublisher: OwnID.RegistrationPublisher) {
eventsPublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] event in
switch event {
case .success(let event):
switch event {
// Event when user successfully
// finishes Skip Password
// in OwnID Web App
case .readyToRegister:
// If needed, ask user to enter
// email (mandatory) and call
// OwnID.FlowsSDK.RegisterView.ViewModel.
// register(with email: String)
// to finish registration.
// To pass additional parameters,
// such as first name, use
// the same approach as in Gigya
let nameValue = "{ \"firstName\": \"\(firstName)\" }"
let paramsDict = ["profile": nameValue]
let params = OwnID.GigyaSDK.Registration.Parameters(parameters: paramsDict)
ownIDViewModel.register(with: email, registerParameters: params)
// Event when OwnID creates Gigya
// account and logs in user
case .userRegisteredAndLoggedIn:
// User is registered and logged in with OwnID
case .loading:
// Display loading indicator according to your designs
case .resetTapped:
// User tapped activeted button. Rest any data if
// needed.
}
case .failure(let error):
// Handle OwnID.CoreSDK.Error here
// For an example of handling an interruption,
// see Errors section of this doc
}
}
.store(in: &bag)
}
}
我们建议在用户输入有效的电子邮件时显示工具提示。工具提示是一种弹出窗口,为用户提供如何使用OwnID按钮的提示。您可以使用自己的电子邮件验证,以及自定义逻辑来显示此建议弹出窗口。
例如,当用户输入有效的电子邮件时,我们使用OwnID视图模型中的绑定来显示工具提示。
// recommended approach is to subscribe to publised property of email and use
// .removeDuplicates() along with .debounce
ownIDViewModel.shouldShowTooltip = ownIDViewModel.shouldShowTooltipEmailProcessingClosure(emailValue)
//...
重要:必须对ownIDViewModel.register
函数进行调用以响应.readyToRegister
事件。该ownIDViewModel.register
函数最终调用Gigya的标准函数createUser(withEmail: password:)
以注册Gigya用户,因此您无需自己调用此Gigya函数。
添加OwnID视图
将OwnID视图插入到您的视图层会导致在应用程序中显示OwnID按钮。当用户选择OwnID按钮时,SDK会在应用程序内Safari浏览器中显示,与用户进行交互。创建此视图的代码接受OwnID视图模型作为其参数。建议传递用户的电子邮件绑定以正确创建账户。
建议将按钮的高度设置与文本字段相同,并在启用ownID时禁用文本字段。
//Put RegisterView inside your main view, preferably besides password field
var body: some View {
OwnID.GigyaSDK.createRegisterView(viewModel: viewModel.ownIDViewModel, email: usersEmail)
}
实现登录屏幕
实现登录屏幕的过程与实现注册屏幕的过程非常类似。当用户在登录屏幕上选择“跳过密码”并如果用户之前已设置OwnID身份验证时,允许他们使用OwnID登录。
类似于注册屏幕,您可以通过包括OwnID视图将“跳过密码”添加到应用程序的登录屏幕中。在这种情况下,它是OwnID.LoginView
。此OwnID视图有自己的视图模型,OwnID.LoginView.ViewModel
。
自定义视图模型
您需要创建一个视图模型实例,OwnID.LoginView.ViewModel
,供OwnID登录视图使用。在您的ViewModel层中输入
final class MyLogInViewModel: ObservableObject {
// MARK: OwnID
let ownIDViewModel = OwnID.GigyaSDK.loginViewModel(instance: <Your Instance Of Gigya>)
}
创建此OwnID视图模型后,您的ViewModel层应监听来自OwnID事件发布者的事件,这将使您的应用程序了解根据用户与“跳过密码”选项的交互应采取哪些操作。只需将以下内容添加到现有的ViewModel层中,以订阅OwnID事件发布者并响应事件。
final class MyLogInViewModel: ObservableObject {
// MARK: OwnID
let ownIDViewModel = OwnID.GigyaSDK.loginViewModel(instance: <Your Instance Of Gigya>)
init() {
subscribe(to: ownIDViewModel.eventPublisher)
}
func subscribe(to eventsPublisher: OwnID.LoginPublisher) {
eventsPublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] event in
switch event {
case .success(let event):
switch event {
// Event when user who previously set up
// OwnID logs in with Skip Password
case .loggedIn:
// User is logged in with OwnID
case .loading:
// Display loading indicator according to your designs
}
case .failure(let error):
// Handle OwnID.CoreSDK.Error here
}
}
.store(in: &bag)
}
}
添加OwnID视图
将OwnID视图插入到您的视图层中,将在您的应用程序中显示“跳过密码”选项。当用户选择“跳过密码”时,SDK将打开一个表单与用户进行交互。建议将OwnID视图,OwnID.LoginView
,放置在密码文本框之后。创建此视图的代码接受OwnID视图模型作为其参数。建议您传递用户的电子邮件绑定以正确创建账户。
//Put LoginView inside your main view, preferably below password field
var body: some View {
//...
// User's email binding `$viewModel.email` is used to display identity
// name when logging in. Additionally, this email is used to get
// information if user already has OwnID account
OwnID.GigyaSDK.createLoginView(viewModel: viewModel.ownIDViewModel,
usersEmail: $viewModel.email)
//...
}
默认情况下,每当登录视图显示时,都会出现工具提示弹出窗口。使用shouldShowTooltip
禁用工具提示的显示。为此,请使用分配false
。
错误
SDK中的所有错误都具有OwnID.CoreSDK.Error
类型。您可以使用它们,例如,要求用户执行操作。
以下是一些可能的错误
switch error {
case .unsecuredHttpPassed:
print("unsecuredHttpPassed")
case .notValidRedirectionURLOrNotMatchingFromConfiguration:
print("notValidRedirectionURLOrNotMatchingFromConfiguration")
case .emailIsInvalid:
print("emailIsInvalid")
case .flowCancelled:
print("flowCancelled")
case .statusRequestResponseIsEmpty:
print("statusRequestResponseIsEmpty")
case .statusRequestFail(underlying: let underlying):
print("statusRequestFail: \(underlying)")
case .plugin(let pluginError):
print("plugin: \(pluginError)")
}
中断
以下是一个处理中断的示例
case .failure(let ownIDSDKError):
switch ownIDSDKError {
case .plugin(let gigyaPluginError):
if let gigyaSDKError = gigyaPluginError as? OwnID.GigyaSDK.Error<Your Account Protocol Of Gigya> {
switch gigyaSDKError {
case .login(let loginError):
switch loginError.interruption {
case .pendingVerification:
print("pendingVerification")
default:
break
}
default:
break
}
}
default:
break
}
处理Gigya请求数据
以下示例展示了如果发生错误时,如何从Gigya SDK获取请求数据
case .failure(let error):
switch error {
case .plugin(let gigyaPluginError):
if let gigyaSDKError = gigyaPluginError as? OwnID.GigyaSDK.Error<GigyaAccount> {
switch gigyaSDKError {
case .gigyaSDK(let error, let dataDictionary):
switch error {
case .gigyaError(let model):
//handling the data
print(dataDictionary)
print(model.errorMessage)
default: break
}
default:
break
}
}
default:
break
}
高级配置
按钮外观
可以通过传入OwnID.UISDK.VisualLookConfig
设置按钮外观设置。此外,您还可以在OwnID.UISDK.TooltipVisualLookConfig
中重写工具提示出现的默认行为或其他设置。通过传递widgetPosition
,或
文本视图将相应地更改其位置。
let tooltipConfig = OwnID.UISDK.TooltipVisualLookConfig(backgroundColor: .pink,
borderColor: .accentColor)
let config = OwnID.UISDK.VisualLookConfig(biometryIconColor: .red,
shadowColor: .cyan,
tooltipVisualLookConfig: tooltipConfig,
widgetPosition: .start)
OwnID.GigyaSDK.createLoginView(viewModel: ownIDViewModel,
usersEmail: usersEmail,
visualConfig: config)
Configure函数的替代语法 🎛
如果您遵循了推荐,将 OwnIDConfiguration.plist
添加到项目中,只需调用 configure()
函数而不传递任何参数,就可以初始化 SDK。如果您没有遵循这个推荐,您仍然可以使用以下调用之一来初始化 SDK。请记住,这些调用应该在您的应用的 @main
App
结构中执行。
OwnID.GigyaSDK.configure(plistUrl: plist)
明确提供了 OwnID 配置文件的路径,其中plist
是文件路径。OwnID.GigyaSDK.configure(appID: String, redirectionURL: URL)
明确定义了配置选项而不是使用 PLIST 文件。服务器应用 ID 是您 OwnID 应用独有的,可在 OwnID 控制台 获取。重定向 URL 是您应用的重定向 URL,包括其自定义方案。
OwnID Web App 语言
默认情况下,OwnID Web App 会根据用户在系统设置的设备区域设置启动带有语言标签列表(格式正确的 IETF BCP 47 语言标签)。您可以通过传递包含在数组中的语言来覆盖此行为并手动设置 Web App 语言列表。示例
OwnID.GigyaSDK.createRegisterView(viewModel: viewModel.ownIDViewModel,
webLanguages: OwnID.CoreSDK.Languages.init(rawValue: ["he"]))
手动调用 OwnID 流程
作为一种替代方案,除了 OwnID 按钮外,还可以使用自定义视图来调用功能。简而言之,它与 ownIDViewModel
中的行为相同,只是提供了您的自定义视图。
创建简单的 PassthroughSubject
。在创建自定义视图后,通过这个 PassthroughSubject
发送 void 动作。在您的 viewModel
中,让 ownIDViewModel
订阅这个新创建的发布者。
ownIDViewModel.subscribe(to: self.buttonPressedPublisher.eraseToAnyPublisher())
良好的实践是为 ownIDViewModel
传递闭包,以便它能将电子邮件传递到 Web 应用。分配电子邮件闭包
ownIDViewModel.getEmail = { self.email }
此外,您可以通过调用 ownIDViewModel.resetState()
来重置视图。
日志记录
您可以通过调用 OwnID.startDebugConsoleLogger()
来启用控制台日志记录。
许可协议
Apache License
Version 2.0, January 2004
https://apache.ac.cn/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Copyright 2022 OwnID INC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.