$npx -y skills add dpearson2699/swift-ios-skills --skill musickitIntegrate Apple Music playback, catalog search, and Now Playing metadata using MusicKit and MediaPlayer. Use when adding music search, Apple Music subscription flows, queue management, playback controls, remote command handling, or Now Playing info to iOS apps.
| 1 | # MusicKit |
| 2 | |
| 3 | Search the Apple Music catalog, manage playback with `ApplicationMusicPlayer`, |
| 4 | check subscriptions, and publish Now Playing metadata via `MPNowPlayingInfoCenter` |
| 5 | and `MPRemoteCommandCenter`. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Setup](#setup) |
| 10 | - [Workflow](#workflow) |
| 11 | - [Authorization](#authorization) |
| 12 | - [Catalog Search](#catalog-search) |
| 13 | - [Subscription Checks](#subscription-checks) |
| 14 | - [Playback with ApplicationMusicPlayer](#playback-with-applicationmusicplayer) |
| 15 | - [Queue Management](#queue-management) |
| 16 | - [Now Playing Info](#now-playing-info) |
| 17 | - [Remote Command Center](#remote-command-center) |
| 18 | - [Common Mistakes](#common-mistakes) |
| 19 | - [Review Checklist](#review-checklist) |
| 20 | - [References](#references) |
| 21 | |
| 22 | ## Workflow |
| 23 | |
| 24 | 1. Verify the MusicKit App Service, bundle identifier, purpose string, and background-audio mode before debugging code. |
| 25 | 2. Request authorization, then model every non-authorized state explicitly. |
| 26 | 3. Search or load catalog content and check `MusicSubscription.current` before queueing playback. |
| 27 | 4. Choose `ApplicationMusicPlayer` for app-scoped playback; wire Now Playing and remote commands only when the app owns those surfaces. |
| 28 | 5. Test authorized, denied, unsubscribed, offline, queue failure, interruption, and track-change states. Fix the smallest failing layer, restore the fixture, and rerun the same state matrix. |
| 29 | |
| 30 | ## Setup |
| 31 | |
| 32 | ### Project Configuration |
| 33 | |
| 34 | 1. Enable the **MusicKit App Service** for the app's explicit bundle ID in the Apple Developer portal so MusicKit can generate developer tokens automatically. |
| 35 | 2. Add `NSAppleMusicUsageDescription` to Info.plist explaining why the app accesses the user's media library. |
| 36 | 3. For background playback, add the `audio` background mode to `UIBackgroundModes`. |
| 37 | |
| 38 | ### Imports |
| 39 | |
| 40 | ```swift |
| 41 | import MusicKit // Catalog, auth, playback |
| 42 | import MediaPlayer // MPRemoteCommandCenter, MPNowPlayingInfoCenter |
| 43 | ``` |
| 44 | |
| 45 | ## Authorization |
| 46 | |
| 47 | Request permission before accessing the user's music data or playing Apple Music |
| 48 | content. `request()` presents Apple's consent dialog when necessary; use |
| 49 | `currentStatus` to read the current setting without prompting. |
| 50 | |
| 51 | ```swift |
| 52 | func requestMusicAccess() async -> MusicAuthorization.Status { |
| 53 | let status = await MusicAuthorization.request() |
| 54 | switch status { |
| 55 | case .authorized: |
| 56 | // Full access to MusicKit APIs |
| 57 | break |
| 58 | case .denied, .restricted: |
| 59 | // Show guidance to enable in Settings |
| 60 | break |
| 61 | case .notDetermined: |
| 62 | break |
| 63 | @unknown default: |
| 64 | break |
| 65 | } |
| 66 | return status |
| 67 | } |
| 68 | |
| 69 | // Check current status without prompting |
| 70 | let current = MusicAuthorization.currentStatus |
| 71 | ``` |
| 72 | |
| 73 | ## Catalog Search |
| 74 | |
| 75 | Use `MusicCatalogSearchRequest` to search the Apple Music catalog. Catalog lookup |
| 76 | can fetch Apple Music resources, but playback of subscription catalog content |
| 77 | must still be gated on `MusicSubscription.current.canPlayCatalogContent`. |
| 78 | |
| 79 | ```swift |
| 80 | func searchCatalog(term: String) async throws -> MusicItemCollection<Song> { |
| 81 | var request = MusicCatalogSearchRequest(term: term, types: [Song.self]) |
| 82 | request.limit = 25 |
| 83 | |
| 84 | let response = try await request.response() |
| 85 | return response.songs |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | ### Displaying Results |
| 90 | |
| 91 | ```swift |
| 92 | for song in songs { |
| 93 | print("\(song.title) by \(song.artistName)") |
| 94 | if let artwork = song.artwork { |
| 95 | let url = artwork.url(width: 300, height: 300) |
| 96 | // Load artwork from url |
| 97 | } |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | ## Subscription Checks |
| 102 | |
| 103 | Check whether the user has an active Apple Music subscription before offering playback features. |
| 104 | |
| 105 | ```swift |
| 106 | func checkSubscription() async throws -> Bool { |
| 107 | let subscription = try await MusicSubscription.current |
| 108 | return subscription.canPlayCatalogContent |
| 109 | } |
| 110 | |
| 111 | // Observe subscription changes |
| 112 | func observeSubscription() async { |
| 113 | for await subscription in MusicSubscription.subscriptionUpdates { |
| 114 | if subscription.canPlayCatalogContent { |
| 115 | // Enable full playback UI |
| 116 | } else { |
| 117 | // Show subscription offer |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ### Offering Apple Music |
| 124 | |
| 125 | Present the Apple Music subscription offer sheet when the user is not subscribed. |
| 126 | Check `canBecomeSubscriber` first, and pass `MusicSubscriptionOffer.Options` or |
| 127 | `onLoadCompletion` when the sheet needs contextual metadata or load-error handling. |
| 128 | |
| 129 | ```swift |
| 130 | import MusicKit |
| 131 | import SwiftUI |
| 132 | |
| 133 | struct MusicOfferView: View { |
| 134 | @State private var showOffer = false |
| 135 | |
| 136 | var body: some View { |
| 137 | Button("Subscribe to Apple Music") { |
| 138 | Task { |
| 139 | let subscription = try? await MusicSubscription.current |
| 140 | showOffer = subscription?.canBecomeSubscriber == true |
| 141 | } |
| 142 | } |
| 143 | .musicSubscriptionOffer( |
| 144 | isPresented: $showOffer, |
| 145 | options: .defaul |