Dreams iOS SDK
需求
- iOS 10.3+
- Swift 5.0+
安装
Cocoapods
要将Dreams集成到项目中使用Cocoapods,将Dreams
添加到您的Podfile
。
pod 'Dreams'
然后运行以下命令
$ pod install
手动
如果您不希望使用Cocoapods,可以直接通过将Sources
文件夹中的文件拖入项目来集成Dreams。
使用
- 在你的
AppDelegate
中导入Dreams
import Dreams
- 在你的
AppDelegate
的application:didFinishLaunchingWithOptions:
中添加以下内容
let configuration = DreamsConfiguration(clientId: "your_client_id", baseURL: URL(string: "your_base_url")!)
Dreams.configure(configuration)
- 创建一个
DreamsViewController
实例。这是一个示例,你可以根据需要在 Storyboards 或 xib 中使用它
let viewController = DreamsViewController()
- 设置代表并实现
DreamsDelegate
方法
viewController.use(delegate: self)
- 准备用户凭据
let userCredentials = DreamsCredentials(idToken: "idToken")
- 呈现 ViewController(作为全屏模态),然后启动 Dreams
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true) {
viewController.launch(with: userCredentials, locale: Locale.current) { result in
switch result {
case .success:
() // Dreams did launch successfully
case .failure(let launchError):
switch launchError {
case .alreadyLaunched:
() // You cannot launch Dreams when launching is in progess
case .invalidCredentials:
() // The provided credentials were invalid
case .httpErrorStatus(let httpStatus):
print(httpStatus) // The server returned a HTTP error status
case .requestFailure(let error):
print(error) // Other server errors (NSError instance)
}
}
}
}
位置
在 Dreams 中使用特定位置启动
viewController.launch(with: userCredentials, locale: Locale.current, location: "/some/location") { result in }
在启动后导航到 Dreams 中的位置
viewController.navigateTo(location: "/some/location")
安全区域
Dreams 界面正在利用安全区域布局向导信息来调整界面以适应不同的屏幕。请勿在界面构建器中禁用 Use Safe Area Layout Guides
当 DreamsViewController
作为子视图控制器呈现时,请勿修改父视图控制器的 layoutMargins
或 directionalLayoutMargins
DreamsDelegate
此方法在凭据通过 launch(with:locale:)
接收后过期且需要生成新的凭据时被调用。
func handleDreamsCredentialsExpired(completion: @escaping (String) -> Void) {
// Call `completion` when account provision is initiated.
completion(DreamsCredentials(idToken: "newtoken"))
}
此方法在发生跟踪事件时被调用。
func handleDreamsTelemetryEvent(name: String, payload: [String : Any]) {
print("Telemetry event received: \(name) with payload: \(payload)")
}
此方法在主应用预期启动账户意向时被调用。
func handleDreamsAccountProvisionInitiated(completion: @escaping () -> Void) {
// Call `completion` when account provision is initiated.
// You can store the `completion` in a property to call it later.
completion()
}
此方法在用户完成与Dreams的交互后,界面应该被隐藏时被调用。
func handleExitRequest() {
// For example:
presentedViewController?.dismiss(animated: true, completion: nil)
}
单元测试
您可以使用 fastlane 运行测试。
要手动运行单元测试,请使用以下命令
xcodebuild -workspace "./Dreams.xcworkspace" -scheme "DreamsTests" -destination "platform=iOS Simulator,name=iPhone 8,OS=14.3" build-for-testing test