What's the difference between MusicItemID("1603171516")
and MusicItemID("i.KoJeg8VCZLNZZq")?
Because when fetching [Track] of the Album when to chose
let id = MusicItemID("i.KoJeg8VCZLNZZq")
var request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: id)
request.properties = [.tracks]
let response = try await request.response()
guard let tracks = response.items.first?.tracks else { return }
albumTracks = tracks
It returns error ⬇️
MusicItemID("1603171516")
is an identifier from the Apple Music catalog, whereas the other one MusicItemID("i.KoJeg8VCZLNZZq")
is an identifier from your music library. So, when you use a catalog request for a user library track, it will give an error.
You can use the Apple Music API and MusicDataRequest to overcome this:
guard let url = URL(string: "https://api.music.apple.com/v1/me/library/albums/i.KoJeg8VCZLNZZq/tracks") else { return }
let request = MusicDataRequest(urlRequest: .init(url: url))
let response = try await request.response()
let tracks = try JSONDecoder().decode(MusicItemCollection<Track>.self, from: response.data)
Or, you can use the new structure MusicLibraryRequest
introduced in iOS 16:
var request = MusicLibraryRequest<Album>()
request.filter(matching: \.id, equalTo: "i.KoJeg8VCZLNZZq")
let response = try await request.response()