Voucher 项目使用教程
1. 项目介绍
Voucher 是一个简单的库,旨在通过 iOS 应用程序轻松实现 tvOS 应用程序的认证。它利用 Bonjour 技术在本地网络中查找其他设备,并通过 AWDL(Apple Wireless Direct Link)进行通信。Voucher 允许用户通过 iOS 应用程序进行认证,从而避免了在 tvOS 设备上输入繁琐的凭证。
2. 项目快速启动
安装 Voucher
Voucher 可以通过 Carthage 和 CocoaPods 进行安装,也可以手动安装。
使用 Carthage 安装
在 Cartfile
中添加以下内容:
github "rsattar/Voucher"
然后运行 carthage update
。
使用 CocoaPods 安装
在 Podfile
中添加以下内容:
pod 'Voucher'
然后运行 pod install
。
手动安装
-
克隆 Voucher 仓库到本地:
git clone https://github.com/rsattar/Voucher.git
-
将 Voucher 子文件夹中的源文件复制到你的项目中。
在 tvOS 应用中使用 Voucher
在你的 tvOS 应用中,当用户需要认证时,创建一个 VoucherClient
实例并启动它:
import Voucher
func startVoucherClient() {
let uniqueId = "SomethingUnique"
self.voucher = VoucherClient(uniqueSharedId: uniqueId)
self.voucher.startSearchingWithCompletion { [unowned self] authData, displayName, error in
if authData != nil {
// 用户在 iOS 应用中授予了权限
self.authenticationSucceeded(authData, from: displayName)
} else {
self.authenticationFailed()
}
}
}
在 iOS 应用中使用 Voucher
如果你的 iOS 应用有认证凭证,它应该启动一个 VoucherServer
,以便响应任何登录请求。建议在用户登录时启动服务器:
import Voucher
func startVoucherServer() {
let uniqueId = "SomethingUnique"
self.server = VoucherServer(uniqueSharedId: uniqueId)
self.server.startAdvertisingWithRequestHandler { (displayName, responseHandler) -> Void in
let alertController = UIAlertController(title: "Allow Auth", message: "Allow \"\(displayName)\" access to your login", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Not Now", style: .cancel, handler: { action in
responseHandler(nil, nil)
}))
alertController.addAction(UIAlertAction(title: "Allow", style: .default, handler: { action in
let authData = "THIS IS AN AUTH TOKEN".data(using: .utf8)
responseHandler(authData, nil)
}))
self.present(alertController, animated: true, completion: nil)
}
}
3. 应用案例和最佳实践
应用案例
Voucher 适用于需要用户在 tvOS 设备上进行认证的应用场景。例如,视频流媒体应用、游戏应用等,用户可以通过 iOS 设备轻松登录,避免了在 tvOS 设备上输入复杂凭证的麻烦。
最佳实践
- 使用令牌而非密码:建议在认证过程中使用 OAuth 令牌或生成单次使用的令牌,而不是直接传递密码。
- 提供备用登录选项:根据 App Store 提交指南,必须在登录界面提供手动输入的选项,并提示用户可以通过 iOS 应用进行登录。
4. 典型生态项目
Voucher 可以与其他依赖 Bonjour 技术的项目结合使用,例如:
- HomeKit:用于智能家居设备的发现和控制。
- AirDrop:用于设备之间的文件共享。
- AirPlay:用于将内容从 iOS 设备投射到 Apple TV。
这些项目与 Voucher 共享相同的底层技术,可以相互补充,提升用户体验。