|
| 1 | + |
| 2 | +   |
| 3 | + |
| 4 | +**ControlKit** is a lightweight Swift Package enabling control of media playback and system volume for third-party |
| 5 | +apps. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +To install **ControlKit**, add it as a dependency to your Xcode project by going to |
| 10 | + |
| 11 | +``` |
| 12 | +File > Add Package Dependencies... |
| 13 | +``` |
| 14 | + |
| 15 | +Enter the package URL |
| 16 | + |
| 17 | +``` |
| 18 | +https://github.com/superturboryan/ControlKit.git |
| 19 | +``` |
| 20 | + |
| 21 | +in the search bar: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +This package contrains two libraries: **Control** + **Controllers**. |
| 26 | + |
| 27 | +Select the libraries you want to add to your project's targets: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Requirements |
| 32 | + |
| 33 | +[`SpotifyController`](Sources/Controllers/SpotifyController.swift) requires a **Spotify Client ID |
| 34 | +and Redirect URL** in order to authorize & connect with to the Spotify app. |
| 35 | +You'll need to _create an app_ in the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard) |
| 36 | +in order to get a _client ID_ and register your _redirect URL_. |
| 37 | + |
| 38 | +You'll also need to [define a custom URL scheme (_redirect URL_) for your app](https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app) |
| 39 | +so that Spotify is able to reopen your app after completing the authorization flow. |
| 40 | + |
| 41 | +Add a `URL Type` to the target's `Info.plist`: |
| 42 | + |
| 43 | +<img width="1007" alt="Screenshot 2024-10-29 at 17 31 24" src="https://github.com/user-attachments/assets/895c8092-4a9d-4526-9f85-5da7b868fbc1"> |
| 44 | + |
| 45 | +### Warning 🦺 |
| 46 | + |
| 47 | +**The `Spotify` access token is not persisted across app launch** by default. You must provide an object conforming |
| 48 | +to `DAO<String>` if you want the access to be persisted. |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +### Control |
| 53 | + |
| 54 | +```swift |
| 55 | +import Control |
| 56 | + |
| 57 | +// 🔊 Increment system volume |
| 58 | +Control.Volume.increaseVolume() |
| 59 | + |
| 60 | +// 🕵️♂️ Check if audio is being played (by another app) |
| 61 | +if Control.Playback.isAudioPlaying { |
| 62 | + // 💃🕺 |
| 63 | +} |
| 64 | + |
| 65 | +// ⏭️ Skip to next track (Apple Music only - use Controllers.SpotifyController for Spotify 💚) |
| 66 | +Control.Playback.AppleMusic.skipToNextTrack() |
| 67 | + |
| 68 | +// 🫨 Vibrate |
| 69 | +Control.Haptics.vibrate() |
| 70 | +``` |
| 71 | + |
| 72 | +### Controllers |
| 73 | + |
| 74 | +```swift |
| 75 | +// App.swift |
| 76 | + |
| 77 | +import Controllers |
| 78 | +import SwiftUI |
| 79 | + |
| 80 | +@main struct YourApp: App { |
| 81 | + |
| 82 | + @StateObject var spotify = SpotifyController( |
| 83 | + config: .init( |
| 84 | + clientID: SpotifyConfig.clientID, |
| 85 | + redirectURL: SpotifyConfig.redirectURL |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + var body: some Scene { |
| 90 | + WindowGroup { |
| 91 | + ContentView() |
| 92 | + .environmentObject(spotify) |
| 93 | + .onOpenURL { spotify.setAccessToken(from: $0) } // Parse access token from URL |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func skipToNextSpotifyTrack() { |
| 98 | + spotify.skipToNextTrack() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Secrets.swift 🔐 |
| 103 | +// Don't forget to gitignore this 🙈 |
| 104 | + |
| 105 | +enum SpotifyConfig { |
| 106 | + static let clientID = "<your client id>" |
| 107 | + static let redirectURL = "controlkit://spotify" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Dependencies |
| 112 | + |
| 113 | +📚 [AVFAudio](https://developer.apple.com/documentation/avfaudio) |
| 114 | +📚 [Media Player](https://developer.apple.com/documentation/mediaplayer/) |
| 115 | +📦 [SpotifyiOS](https://github.com/spotify/ios-sdk) |
| 116 | + |
| 117 | +## Contributing |
| 118 | + |
| 119 | +Contributions and feedback are welcome! 🤝 |
| 120 | + |
| 121 | +You can open an Issue or raise a PR. |
| 122 | + |
| 123 | +Emojis required in all commit messages ❤️ |
0 commit comments