Play a .kvk file in an iOS app with the KinetoRuntime Swift package. The examples on this page use Cody, the same file and the same inputs as Web Playback.
Get access
To build from the public source:
- Clone
https://github.com/HBrinkman/kineto-runtime.gitand check the repository’s current prerequisites and compatibility notes. You need macOS, Xcode, Rust and the build tools listed there. - From the clone root, run
tool/refresh_runtime_xcframework.shto generate the device and simulator libraries. - In Xcode, add
runtimes/swift/KinetoRuntimefrom that clone as a local package, then link theKinetoRuntimelibrary. The package requires iOS 15 or later.
Keep the source revision and generated library together. Do not point Xcode at an invented release tag or assume the repository root is a ready-to-download Swift binary package.
Example file
Download cody.kvk, the runtime file the Kineto homepage plays.
Add cody.kvk to your Xcode target, so it’s copied into the app bundle.
| Item | Value |
|---|---|
| State machine id | cfba0cd6-7a60-4851-a8d2-d312b67b5392 |
| Inputs | tapped-left-eye, tapped-right-eye (bool) |
| Clips | Wink left, Idle, Wink right |
For your own file, read these values from the handoff notes, or from stateMachines() and inputs(in:). To get the notes, keep the handoff option on when you export the .kvk.
Show the player in SwiftUI
KinetoPlayer holds the animation, and KinetoPlayerViewController draws it. In SwiftUI, wrap the controller in a UIViewControllerRepresentable.
This example was run on a physical iPad with cody.kvk.
import KinetoRuntime
import SwiftUI
struct KinetoPlayerView: UIViewControllerRepresentable {
let player: KinetoPlayer
func makeUIViewController(context: Context) -> KinetoPlayerViewController {
KinetoPlayerViewController(player: player)
}
func updateUIViewController(_ controller: KinetoPlayerViewController, context: Context) {}
}
struct CodyView: View {
private static let machineID = "cfba0cd6-7a60-4851-a8d2-d312b67b5392"
@State private var player: KinetoPlayer?
@State private var errorMessage: String?
var body: some View {
VStack {
if let player {
KinetoPlayerView(player: player)
.aspectRatio(1, contentMode: .fit)
HStack {
Button("Wink left") { wink("tapped-left-eye") }
Button("Wink right") { wink("tapped-right-eye") }
}
} else if let errorMessage {
Text(errorMessage)
}
}
.task(load)
}
@Sendable private func load() async {
guard player == nil else { return }
do {
guard let url = Bundle.main.url(forResource: "cody", withExtension: "kvk") else {
errorMessage = "cody.kvk is not in the app bundle."
return
}
player = try KinetoPlayer(contentsOf: url)
} catch {
errorMessage = String(describing: error)
}
}
private func wink(_ input: String) {
guard let player else { return }
do {
try player.setBool(true, input: input, in: Self.machineID)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.22) {
try? player.setBool(false, input: input, in: Self.machineID)
}
} catch {
errorMessage = String(describing: error)
}
}
}
Show the player in UIKit
Add the controller as a child view controller.
import KinetoRuntime
import UIKit
final class CodyViewController: UIViewController {
private var player: KinetoPlayer?
override func viewDidLoad() {
super.viewDidLoad()
do {
guard let url = Bundle.main.url(forResource: "cody", withExtension: "kvk") else {
print("cody.kvk is not in the app bundle.")
return
}
let player = try KinetoPlayer(contentsOf: url)
self.player = player
let controller = KinetoPlayerViewController(player: player)
addChild(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
controller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
controller.view.topAnchor.constraint(equalTo: view.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
controller.didMove(toParent: self)
} catch {
print("Kineto could not load the file: \(error)")
}
}
}
What the view controller does
- It starts advancing the player when its view appears, and stops when the view disappears.
- It handles touches. A tap on one of Cody’s eyes makes him wink without any more code. Touch movement also reaches targets that follow the pointer.
- It draws the artwork fitted to its view.
- It shows an error message over the view if the renderer can’t start.
Pass autoplay: false to create it without advancing. You can change this later through isPlaybackEnabled.
Set inputs
Each call names the input and its state machine. activeState(in:) returns the id of the current state.
let machine = "cfba0cd6-7a60-4851-a8d2-d312b67b5392"
try player.setBool(true, input: "tapped-left-eye", in: machine) // Cody blends to Wink.
try player.setBool(false, input: "tapped-left-eye", in: machine) // Cody blends back to Idle.
let state = player.activeState(in: machine)
Files with other input types use these calls:
try player.setTrigger("wave", in: machine)
try player.setNumber(0.5, input: "progress", in: machine) // A value from 0 to 1.
The host can set number inputs; engine and web binding fixtures are tested. The editor’s old runtime v1 skips label was incorrect and has been removed. See Known Limitations for the scope of that evidence.
Control clips
let clips = player.clips() // Each clip has an id, a name and a duration.
try player.selectAnimation(clips[0].id)
try player.play()
try player.pause()
Handle errors
The calls throw KinetoRuntimeError. These are the messages Cody’s file gives:
| Situation | Message |
|---|---|
The data isn’t a .kvk file |
Kineto load failed (3): Unable to load .kvk data |
| The input name is wrong | Kineto operation failed (2): Unknown input name "..." |
| The machine id is wrong | Kineto operation failed (2): Unknown state machine id "..." |
Test on a device
- The file is in the app bundle
- Cody appears when the view opens
- A tap on an eye makes him wink, and he returns to Idle when you let go
- Timing feels right at the device's frame rate
- Performance is acceptable on your slowest supported device