ControlKit is a minimal Swift Package enabling control of media playback and device volume.
Control the device's volume with one line:
Control.Volume.increase() // π π
Add ControlKit as a dependency in your Xcode project:
-
Go to File > Add Package Dependenciesβ¦
-
Enter the package URL in the search bar:
https://github.com/superturboryan/ControlKit.git
- Choose the libraries you want to include:
SpotifyController
requires a Spotify Client ID
and Redirect URL to authorize with & control the Spotify app.
-
Define a custom URL scheme for your app. Add a
URL Type
to the target'sInfo.plist
. -
Create an app in the Spotify Developer Dashboard to get a client ID and register your redirect URL (scheme).
The Spotify access token is not persisted across app launches by default.
You must provide an object conforming to DAO<String>
if you want the access token to be persisted.
import Control
// π Decrement system volume
Control.Volume.decrease()
// π΅οΈββοΈ Check if audio is being played (by another app)
if Control.Playback.isAudioPlaying {
// ππΊ
}
// βοΈ Skip to next track (Apple Music only - use Controllers.SpotifyController for Spotify π)
Control.Playback.AppleMusic.skipToNextTrack()
// 𫨠Vibrate
Control.Haptics.vibrate()
// App.swift
import Controllers
import SwiftUI
@main struct YourApp: App {
@StateObject var spotify = SpotifyController(
config: .init(
clientID: Secrets.clientID,
redirectURL: "controlkit://spotify"
)
)
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(spotify)
.onOpenURL { spotify.setAccessToken(from: $0) } // Parse access token from URL
}
}
func skipToNextSpotifyTrack() {
spotify.skipToNextTrack()
}
}
// Secrets.swift π
// Don't forget to gitignore this π
enum Secrets {
static let clientID = "<your client id>"
}
π AVFAudio
π Media Player
π¦ SpotifyiOS
Contributions and feedback are welcome! π§βπ»π©βπ»
Here are a few guidelines:
- You can open an Issue or raise a PR π€
- Commit messages should contain emojis β€οΈ and be signed π
- Workflows should be green π’
main
should be linear π