4

I know how to get MPMediaQuery's title by:

MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];

NSArray *items = [playlistsQuery collections];

MPMediaPlaylist *myPlaylist = items.firstObject;

NSLog(@"%@",myPlaylist.name); //"New playlist title"

Does anyone know how to access MPMediaPlaylist's cover & description?

Screenshot - User created Playlist

bryanjclark
  • 6,247
  • 2
  • 35
  • 68
Zuyin XU
  • 43
  • 1
  • 5

2 Answers2

1

Getting description is easy, just use

var descriptionText: String? { get }

from MPMediaPlaylist

To get user defined image you have to use private API, here's an extension you can use to get image stored on disk, or get image's URL:

extension MPMediaPlaylist {
    /**
     User selected image for playlist stored on disk.
     */
    var userImage: UIImage? {
        guard let catalog = value(forKey: "artworkCatalog") as? NSObject else {
            return nil
        }

        let sel = NSSelectorFromString("bestImageFromDisk")

        guard catalog.responds(to: sel),
            let value = catalog.perform(sel)?.takeUnretainedValue(),
            let image = value as? UIImage else {
            return nil
        }
        return image
    }

    /**
     URL for playlist's image.
     */
    var imageUrl: URL? {
        if let catalog = value(forKey: "artworkCatalog") as? NSObject,
            let token = catalog.value(forKey: "token") as? NSObject,
            let url = token.value(forKey: "availableArtworkToken") as? String {
            return URL(string: "https://is2-ssl.mzstatic.com/image/thumb/\(url)/260x260cc.jpg")
        }
        return nil
    }
}

As always, keep in mind that it can stop working anytime when Apple changes stuff under the hood or your app might get rejected from App Store.

Adam
  • 1,776
  • 1
  • 17
  • 28
0

I haven't figured out the custom playlist artwork, but I do know how to get the playlist description! Here's the answer in Swift 3 syntax:

let allPlaylists = MPMediaQuery.playlists().collections
let playlist = allPlaylists.first

// I dunno how to get the custom playlist artwork, 
// but you can get artwork from items in the playlist:
let artwork = playlist?.representativeItem?.artwork

// The description is here:
let description = playlist?.descriptionText
bryanjclark
  • 6,247
  • 2
  • 35
  • 68
  • I noticed there are several new attributes become available since iOS9.3: descriptionText, authorDisplayname – Zuyin XU Mar 02 '17 at 17:07