0

How can I only retrieve playlists that are editable? Or in other words how can I retrieve playlists that are only created by the user?


We can get the playlists of the user by the following code block:

 var request = MusicLibraryRequest<Playlist>()

 request.sort(by: \.lastPlayedDate, ascending: false)

 let response = try await request.response()


Also, we can add a track to the playlist with the following code block:

TASK {
     do {
         try await MusicLibrary.shared.add(track, to: playlist)
      } catch (let error) {
         print(error)
     }
}


However not all playlists are editable. For instance if the playlist created by Apple or another Apple Music user, we receive an error while adding track to the playlist because we don't have a permission to do it.


I receive the following error: Error Domain=MPErrorDomain Code=5 "The requested action is not supported" UserInfo={NSLocalizedDescription=The requested action is not supported}

1 Answers1

0

I haven't figured out how to do with the new MusicLibraryRequest so I wrote my own implementation. Hope it helps.

The first step is to create a MLibraryPlaylist that has the canEdit property, because that's what you want:

import Foundation
import MusicKit

public struct MLibraryPlaylist: Codable, MusicItem {
  public let id: MusicItemID
  public let attributes: Attributes

  public struct Attributes: Codable, Sendable {
    public let canEdit: Bool
    public let name: String
    public let isPublic: Bool
    public let hasCatalog: Bool
    public let playParams: PlayParameters
    public let description: Description?
    public let artwork: Artwork?
  }

  public struct Description: Codable, Sendable {
    public let standard: String
  }

  public struct PlayParameters: Codable, Sendable {
    public let id: MusicItemID
    public let isLibrary: Bool
    public let globalID: MusicItemID?

    enum CodingKeys: String, CodingKey {
      case id, isLibrary
      case globalID = "globalId"
    }
  }

  public var globalID: String? {
    attributes.playParams.globalID?.rawValue
  }
}

Then, fetch the library playlists using the Apple Music API and decode the data as a collection of MLibraryPlaylist instead of Playlist:

public typealias LibraryPlaylists = MusicItemCollection<LibraryPlaylist>

let libraryPlaylistsURL = URL(string: "https://api.music.apple.com/v1/me/library/playlists")

guard let libraryPlaylistsURL else { return }

let request = MusicDataRequest(urlRequest: URLRequest(url: libraryPlaylistsURL))
let response = try await request.response()
let playlists = try JSONDecoder().decode(LibraryPlaylists.self, from: response.data)

guard let playlist = playlists.first else { return }

print(playlist)
print(playlist.attributes.canEdit)

I hope it solves your problem! I know it is not as good as the native implementation where you can also filter by the last played date.

Rudrank Riyam
  • 588
  • 5
  • 13
  • Thanks man, this solution is the only way so far. I hoped they would add or improve the MusicKit in WWDC 23 but didn't happen :D Even though "canEdit" is true, with MusicKit you can only edit the playlists that are created by your app. So I got an error for the playlists that I created in Apple's Music app. Is it possible to edit playlists that are not created by our app with API? (https://api.music.apple.com/v1/me/library/playlists/{id}/tracks) – Ali Mert Jul 12 '23 at 19:11
  • I looked into MusicKit APIs and there is no edit feature :D Only MusicKit has it with limited functionality :/ – Ali Mert Jul 12 '23 at 22:13
  • Yep, not possible to update the playlist that are not created by your app. :( – Rudrank Riyam Jul 14 '23 at 04:32