16

Posted this on Apple with no luck, but now that the iOS 6 NDA is up, hoping more eyes will see it here.

I am attempting to modify an app to only allow a user to select music that has been downloaded locally. I have the following code under iOS 6 GM:

 MPMediaPickerController* mpc = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];

mpc.allowsPickingMultipleItems = YES;
mpc.modalPresentationStyle = UIModalPresentationCurrentContext;
mpc.showsCloudItems = NO;

[self presentViewController:mpc animated:YES completion:nil];

From the documentation:

The default behavior for a media item picker is YES, which means the the picker shows available iCloud items. A media item is considered an iCloud item if it is available via iTunes Match and is not already stored on the device.

I take this to mean that if iTunes Match is enabled, only items that have been downloaded to the device will show in the picker, however I always see the entire iTunes Match library. I filed a radar for this, because it seems like a serious bug. If anyone can tell me otherwise, I'd love to know what I'm missing here.

6
  • What's the Radar number? I'll dup it. It's still broken on 6.0.1. Commented Nov 2, 2012 at 16:14
  • Still broken in 3/6/2013. Was fighting this one myself. Glad to see I'm not alone. Commented Mar 6, 2013 at 5:26
  • still broken on 6.1.2. but when it's an iCloud/iTunes Match item the property MPMediaItemPropertyAssetURL of MPMediaItem should return nil. Commented Mar 7, 2013 at 17:55
  • I filed a bug too #14022908.
    – jessecurry
    Commented May 30, 2013 at 14:05
  • Has there been a fix for this since iOS7? I've been using what the below answers have suggested, but its not the ideal solution.
    – Sean
    Commented Oct 23, 2013 at 15:00

4 Answers 4

11

This seems to be an OS problem.

Using picker.showsCloudItems = NO; correctly shows fewer songs, instead of the whole list... The songs listed there are songs that either were manually downloaded in the Music app or songs that were streamed and therefore cached.

The problem, at least in my case, is dealing with the cached ones.

If I select a song that was manually downloaded the value of MPMediaItemPropertyIsCloudItem is NO, which is correct. I can also access the asset's URL through the MPMediaItemPropertyAssetURL property.

On the other hand, selecting a song that was cached returns YES on MPMediaItemPropertyIsCloudItem and nil on MPMediaItemPropertyAssetURL, making the song virtually useless to me.

Sorry I don't have an actual answer but I don't have enough reputation to simply comment.

Hope my 2 cents help somehow, but it truly seems to me that this issue can only be resolved by Apple in a future update.

4

A better solution to test if an item comes from iCloud in the didPickMediaItems delegate:

     MPMediaItem *selectedItem = [selectedItems objectAtIndex:0];

     if (![[selectedItem valueForProperty:MPMediaItemPropertyIsCloudItem] boolValue])

You don't really need to play it, it is more efficient to use the embedded property in the MPMediaItem.

2

I had this same problem. Although I was unable to hide the items, here's a good workaround that I used to prevent people from being able to select them. Inside didPickMediaItems, you should temporarily load it into an AVPlayerItem and then just check the validity of that item like so:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
   MPMediaItem *selectedItem = [[mediaItemCollection items]objectAtIndex:0];
   NSURL *tempURL = [selectedItem valueForProperty:MPMediaItemPropertyAssetURL];
   AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithURL:tempURL];

   if(playerItem.loadedTimeRanges==NULL)
   {
     UIAlertView *alert=[[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Invalid Song Choice",NULL) message:NSLocalizedString(@"Please choose a song that is local to your phone.",NULL) delegate:self cancelButtonTitle:NSLocalizedString(@"Okay",NULL) otherButtonTitles:nil]autorelease];
     [alert show];
     [playerItem release];
   }
   else
   { 
       NSLog(@"Your good to go...do whatever you want with the local song");
   }
}
2
  • woooooah.. can someone say "expensive"?
    – abbood
    Commented Jul 14, 2015 at 9:30
  • Thanks! for point out this. You can directly check if the tempurl is nill or not. Commented Apr 19, 2021 at 13:09
-1

It appears to be fixed in iOS 7.

The following code works; iCloud items are not showing:

 MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
 picker.delegate = self;
 picker.allowsPickingMultipleItems   = NO;
 picker.showsCloudItems = NO;
2
  • Just a note: this seems to only work if your deployment target is ≥ 7.0. Commented Feb 25, 2014 at 2:43
  • It seems to be working for me with deployment target 6. Thank you to CocoaAficionado Commented Feb 23, 2016 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.