2

I wish to fetch a MPMediaItemCollection or NSArray of all artists in a user's library. Here's my current code (which obviously doesn't work):

- (void)viewDidLoad
{
    [super viewDidLoad];
    MPMediaQuery *artistsQuery = [MPMediaQuery artistsQuery];
    self.artistsArray = artistsQuery.collections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.artistsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArtistsCell"];
    cell.textLabel.text = [(MPMediaItemCollection *)self.artistsArray[indexPath.row]   valueForProperty:MPMediaPlaylistPropertyName];
    NSLog(@"%@", cell.textLabel.text);
    return cell;
}
Bryan Luby
  • 2,527
  • 22
  • 31
duci9y
  • 4,128
  • 3
  • 26
  • 42

1 Answers1

6

For a list of artists, use MPMediaItemPropertyArtist rather than MPMediaPlaylistPropertyName for the media item property key.

Since your data source is using a MPMediaItemCollection, use the representativeItem property to get the artist title string for each collection.

Then set the textLabel's text to the artist string.

MPMediaItemCollection *artistCollection = self.artistsArray[indexPath.row];
NSString *artistTitle = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
cell.textLabel.text = artistTitle;
Bryan Luby
  • 2,527
  • 22
  • 31
  • Thanks for the answer, it worked a treat! How would you get a picture of the artist as the cell.imageView.image, though? In the Music app, it shows pictures of the artists that aren't the album artworks. – WunDaii May 19 '14 at 16:26
  • If i want to get the artist artwork, how can i get that? – Abdul Yasin Oct 03 '16 at 05:27