8

I am trying to follow a recent post on using a MPMediaPickerControllerDelegate to present a music selection list.

The tutorial is found at this URL:

http://www.justindoan.com/tutorials/

I am using this code:

import UIKit
import MediaPlayer

class ViewController: UIViewController, MPMediaPickerControllerDelegate  {

    var mediapicker1: MPMediaPickerController!

    override func viewDidLoad() {
        super.viewDidLoad()


        let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
        mediaPicker.allowsPickingMultipleItems = false
        mediapicker1 = mediaPicker
        mediaPicker.delegate = self
        self.presentViewController(mediapicker1, animated: true, completion: nil)
        }
}

However I have found that the:

self.presentViewController(mediapicker1, animated: true, completion: nil)

does not work. Unfortunately, Swift 3's suggested automatic solution does not work either:

self.present(mediapicker1, animated: true, completion: nil)

Furthermore, the iOS 10 Beta Release Notes, found at:

https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes

says on page 10 of 18,

An MPMediaPickerController object may not display as expected.

I have spent a great deal of time looking to solve this issue on my own with no success.

Any suggestions?

George Lee
  • 814
  • 18
  • 34
  • Your link is a mirror of beta documentation. You should always use the official documentation from Apple. iOS 10 is released and out of beta. – JAL Sep 27 '16 at 14:23
  • @George Lee, I recently publish an app on [appstore](https://itunes.apple.com/in/app/playmates./id1156484061?mt=8) which present the Music Library With a View Controller for a MPMediaPickerControllerDelegate using Swift 3. Check if you have same requirement, then I can help you in detail(with code as well) – pkc456 Sep 29 '16 at 18:07
  • @pkc456 that would be nice. Could you share the code on this post? – George Lee Sep 30 '16 at 07:16
  • @GeorgeLee, I have written the working code in [answer below](http://stackoverflow.com/a/39788147/988169). I also mention some discrepancies in your code. I am open to ideas. – pkc456 Sep 30 '16 at 09:28

3 Answers3

23

Go through the steps:

  1. Add 'NSAppleMusicUsageDescription' to your Info.plist for the privacy authority.
  2. Make sure your Music app is available in your iPhone. It will not work in the simulator.
jhd
  • 1,243
  • 9
  • 21
  • The plist key `NSAppleMusicUsageDescription` is not necessary for the code in the question to run properly. – JAL Sep 27 '16 at 14:20
  • 3
    @JAL In new iOS10, it will be a blank view comes out. Tested in iPhone6 Plus, iOS10. – jhd Sep 27 '16 at 14:22
  • This answer is correct. Addition of the NSAppleMusicUsageDescription in the info.plist does allow for the MPMediaPickerDelegate to display the music list. It will bring up a Privacy - Media Library Usage the Type: String, the Value: Yes. – George Lee Sep 30 '16 at 10:01
5

As per our discussion over comments, I recently use the MPMediaPickerController in my latest app named playmates. I am sharing the working code with you. I have written the self-explanatory code.

import MediaPlayer

class viewControllerName: UIViewController,MPMediaPickerControllerDelegate {

//Below is Inaction for picking music from media library
    @IBAction func btnMediaPickerAction(_ sender: UIButton) {        
            let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
            mediaPicker.delegate = self
            mediaPicker.allowsPickingMultipleItems = false
            self.present(mediaPicker, animated: true, completion: nil)
        }

// MPMediaPickerController Delegate methods
    func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
            self.dismiss(animated: true, completion: nil)
        }

        func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
            self.dismiss(animated: true, completion: nil)
            print("you picked: \(mediaItemCollection)")//This is the picked media item. 
//  If you allow picking multiple media, then mediaItemCollection.items will return array of picked media items(MPMediaItem)
            }
}

I found below discripencies in your code:

  • use self.present not self.presentViewController
  • No need to create global instance of mediaPicker. As in the delegate method you are getting the instance of MPMediaPickerController
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • This still does not allow me to select a song like it used to allow (brings up a list of songs to select) in iOS10. Unless I am following your instructions incorrectly. – George Lee Sep 30 '16 at 09:39
  • 1
    Add `NSAppleMusicUsageDescription` to your Info.plist. – pkc456 Sep 30 '16 at 10:43
0

Add Privacy - Media Library Usage Description in info.plist

Tushar Katyal
  • 412
  • 5
  • 12