iosswiftmpmediapickercontroller

Failing to use MPMediaPickerController


I am trying to use MPMediaPickerController for the first time, in an iOS application.

Here is the relevant code:

......
import MediaPlayer

class ViewController: UIViewController,...,MPMediaPickerControllerDelegate {
    ......
    var mediPic_VC:MPMediaPickerController!
    ......

    @objc func fireMediaPicker() {
        if mediPic_VC == nil {
            mediPic_VC = MPMediaPickerController(mediaTypes: .anyAudio)
            //mediPic_VC = MPMediaPickerController(mediaTypes: .music)
            mediPic_VC.delegate = self
        }

        self.present(mediPic_VC, animated: true, completion: nil)
    }

    ......

    // MPMediaPickerControllerDelegate protocol implementation.

    func mediaPicker(_ mediaPicker: MPMediaPickerController,
                     didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        print(#function)
    }


    func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
        print(#function)
    }

    // End of MPMediaPickerControllerDelegate protocol implementation.

    ......
}

As one can see I have a function (fireMediaPicker) to bring up the MPMediaPickerController.

But when I run it, the app shows nothing more than a white screen and the debugging console displays the message:

mediaPickerDidCancel(_:)

proving that the function mediaPickerDidCancel of the MPMediaPickerControllerDelegate protocol has been called.

Instead of the white screen I was expecting to see a list of audio items to choose from. Beside, I have no idea why mediaPickerDidCancel is called.

What did I miss in the way I am trying to use MPMediaPickerController?


Solution

  • Add these to your plist:

    <key>NSAppleMusicUsageDescription</key> 
    <string>your own string</string>