The Music Picker

The music picker (MPMediaPickerController) is a view controller (UIViewController) whose view is a self-contained navigation interface in which the user can select a media item. This interface looks very much like the Music app. You have no access to the actual view; you are expected to present the view controller (or, on the iPad, to use a popover).

You can limit the type of media items displayed by creating the controller using initWithMediaTypes:. You can make a prompt appear at the top of the navigation bar (prompt). And you can govern whether the user can choose multiple media items or just one, with the allowsPickingMultipleItems property. New in iOS 6, you can filter out items stored in the cloud (through iTunes Match) by setting showsCloudItems to NO. That’s all there is to it.

While the view is showing, you learn what the user is doing through two delegate methods (MPMediaPickerControllerDelegate):

  • mediaPicker:didPickMediaItems:
  • mediaPickerDidCancel:

How you use these depends on the value of the controller’s allowsPickingMultipleItems:

The controller’s allowsPickingMultipleItems is NO (the default)
Every time the user taps a media item, your mediaPicker:didPickMediaItems: is called, handing you an MPMediaItemCollection consisting of all items the user has tapped so far (including the same item multiple times if the user taps the same item more than once). When the user taps Cancel, your mediaPickerDidCancel: is called.
The controller’s allowsPickingMultipleItems ...

Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.