LetMeWatch is a Swift Package that allows playing and loading AVAsset
s with a server which requires client certificate authentication. Client certificate authentication (CCA) allows servers to authenticate that the client is legitimate and authorized to access the resources. This allows the server to avoid unwanted requests and costs.
An example of this is Cloudflare's new API Shield feature, which makes it easy to require CCA to access resources on your server, avoiding other clients or browsers from loading the resources, which could result in costs to you.
This package contains code adapted from Jared's great blog post on creating a custom AVAssetResourceLoaderDelegate
, available on Github under the MIT License.
LetMeWatch requires iOS 14+ or macOS 11+. It depends on LetMeIn, a package by the same author which performs client certificate authentication.
You can use LetMeWatch as a Swift Package, or add it manually to your project.
Swift Package Manager is a way to add dependencies to your app, and is natively integrated with Xcode.
To add LetMeWatch with SPM, click File
โบ Swift Packages
โบ Add Package Dependency...
, then type in the URL to this Github repo. Xcode will then add the package to your project and perform all the necessary work to build it.
https://github.com/julianschiavo/LetMeWatch
Alternatively, add the package to your Package.swift
file.
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/julianschiavo/LetMeWatch.git", from: "1.0.0")
],
// ...
)
See SPM documentation to learn more.
If you prefer not to use SPM, you can also add LetMeWatch as a normal framework by building the Xcode project from this repository. (See other sources for instructions on doing this.)
Create an instance of an authenticator, then use it as the delegate when performing URLSession
requests.
First, set up the global configuration object for the package.
// Create a certificate file representation
let certificateFile = CertificateFile(fileName: "certificate", password: "12345678")
// Update the global configuration object
LetMeWatchConfiguration.certificateFile = certificateFile
(See the documentation for LetMeIn for more details about supported certificate types and creating CertificateFile
objects.)
Then, use SignedAsset
instead of AVAsset
โand you're done! LetMeWatch will handle loading the asset (byte-by-byte if supported by the server, allowing quicker playback) using client certificate authentication.
// 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)
You can provide an optional error handler to handle errors thrown when loading the asset info or data.
// Remote URL to the asset
let url = ...
// Create a signed asset
let asset = SignedAsset(url: url) { error in
// Handle the error
}
Contributions and pull requests are welcomed by anyone! If you find an issue with LetMeWatch, file a Github Issue, or, if you know how to fix it, submit a pull request.
Please review our Code of Conduct and Contribution Guidelines before making a contribution.
LetMeWatch was created by Julian Schiavo, and available under the MIT License. This package contains code adapted from Jared's great blog post on creating a custom AVAssetResourceLoaderDelegate
, available on Github under the MIT License.
Available under the MIT License. See the License for more info.