Contents

musicPicker(isPresented:title:selection:)

Presents a music picker to select items from the Apple Music catalog and the user’s music library.

Declaration

@MainActor @preconcurrency func musicPicker<Selection>(isPresented: Binding<Bool>, title: Text? = nil, selection: Binding<MusicItemCollection<Selection>>) -> some View where Selection : PickableMusicItem

Parameters

  • isPresented:

    The binding to whether the music picker should be shown.

  • title:

    The title to be shown in the navigation bar.

  • selection:

    The selected music items.

Discussion

Example:

struct MusicPickerButton: View {
    @State private var isShowingMusicPicker = false
    @State private var selectedSongs: MusicItemCollection<Song> = []
    private var title = "Add to Playlist"

    var body: some View {
        Button("Pick one or more songs") {
            isShowingMusicPicker = true
        }
        .musicPicker(
            isPresented: $isShowingMusicPicker,
            title: Text(title), 
            selection: $selectedSongs
        )
        .onChange(of: selectedSongs) { oldValue, newValue in
            let delta = newValue.count - oldValue.count
            print("You selected \(delta) new tracks!")
        }
    }
}

See Also

Interacting with the App Store and Apple Music