LetMeWatch 是一个 Swift 包,允许播放和加载需要客户端证书认证的服务器上的 AVAsset
。客户端证书认证 (CCA) 允许服务器验证客户端是合法且被授权访问资源的。这使得服务器可以避免不必要的请求和成本。
例如,Cloudflare 新推出的 API Shield 功能,可以轻松地要求 CCA 才能访问您服务器上的资源,从而避免其他客户端或浏览器加载资源,这可能会给您带来成本。
此包包含的代码改编自 Jared 关于创建自定义 AVAssetResourceLoaderDelegate
的精彩博文,可在 Github 上以 MIT 许可证获得。
LetMeWatch 需要 iOS 14+ 或 macOS 11+。 它依赖于 LetMeIn,这是同一作者开发的执行客户端证书认证的包。
您可以使用 LetMeWatch 作为 Swift 包,或者手动将其添加到您的项目中。
Swift Package Manager 是一种向您的应用程序添加依赖项的方法,并与 Xcode 原生集成。
要使用 SPM 添加 LetMeWatch,请点击 File
► Swift Packages
► Add Package Dependency...
,然后输入此 Github 仓库的 URL。Xcode 随后会将该包添加到您的项目,并执行所有必要的构建工作。
https://github.com/julianschiavo/LetMeWatch
或者,将该包添加到您的 Package.swift
文件中。
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/julianschiavo/LetMeWatch.git", from: "1.0.0")
],
// ...
)
请参阅 SPM 文档 以了解更多信息。
如果您不想使用 SPM,您也可以通过从此仓库构建 Xcode 项目,将 LetMeWatch 作为普通框架添加。(请参阅其他来源以获取有关执行此操作的说明。)
创建验证器的实例,然后在执行 URLSession
请求时将其用作委托。
首先,设置包的全局配置对象。
// Create a certificate file representation
let certificateFile = CertificateFile(fileName: "certificate", password: "12345678")
// Update the global configuration object
LetMeWatchConfiguration.certificateFile = certificateFile
(有关支持的证书类型和创建 CertificateFile
对象的更多详细信息,请参阅 LetMeIn 的文档。)
然后,使用 SignedAsset
而不是 AVAsset
— 就完成了!LetMeWatch 将处理使用客户端证书认证加载资产(如果服务器支持,则逐字节加载,从而加快播放速度)。
// Remote URL to the asset
let url = ...
// Create a signed asset
let asset = SignedAsset(url: url)
// Use the signed asset in place of an `AVAsset`
// For example, with `VideoPlayer` in SwiftUI
let playerItem = AVPlayerItem(asset: asset)
let avPlayer = AVPlayer(playerItem: playerItem)
let player = VideoPlayer(player: avPlayer)
您可以提供可选的错误处理程序来处理加载资产信息或数据时抛出的错误。
// Remote URL to the asset
let url = ...
// Create a signed asset
let asset = SignedAsset(url: url) { error in
// Handle the error
}
欢迎任何人贡献和提交 pull request!如果您发现 LetMeWatch 存在问题,请提交 Github Issue;或者,如果您知道如何修复它,请提交 pull request。
LetMeWatch 由 Julian Schiavo 创建,并根据 MIT 许可证 提供。此包包含的代码改编自 Jared 关于创建自定义 AVAssetResourceLoaderDelegate
的精彩博文,可在 Github 上以 MIT 许可证获得。
根据 MIT 许可证提供。 有关更多信息,请参阅 许可证。