Crash while presenting a media picker for Music

This is the code I use:

@MainActor func picker()->UIViewController{
        let pickerController = MPMediaPickerController(mediaTypes: .music)
        print(pickerController)
        pickerController.showsCloudItems=true
        pickerController.prompt = NSLocalizedString("Add pieces to queue", comment:"");
        pickerController.allowsPickingMultipleItems=true;
        pickerController.delegate=MPMusicPlayerControllerSingleton.sharedController();
        return pickerController
    }

MainActor
    @IBAction func handleBrowserTapped(_ sender: AnyObject){
        if let pickerController=contentProvider?.picker(){
            self.present(pickerController, animated:true, completion:nil)
        }
    }

And this his is the crash log:


*** Assertion failure in -[MPMediaPickerController_Appex requestRemoteViewController], MPMediaPickerController.m:523
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'remoteViewController cannot be nil -- process will crash inserting in hierarchy. We likely got a nil remoteViewController because Music is crashing.'
*** First throw call stack:
(0x1869cac70 0x183499224 0x1844f9f50 0x1a6c6a060 0x18d45518c 0x18d4cd410 0x103354544 0x10336dccc 0x10338f748 0x103364204 0x103364144 0x186957a64 0x1868e5288 0x1868e41d0 0x22bde7498 0x18c5a5ca0 0x18c510254 0x18c71ce90 0x103854340 0x1038542b0 0x103854430 0x1834f1c1c)
libc++abi: terminating due to uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'remoteViewController cannot be nil -- process will crash inserting in hierarchy. We likely got a nil remoteViewController because Music is crashing.'

Thanks so much for the post.

The crash you are experiencing: remoteViewController cannot be nil... We likely got a nil remoteViewController?

Even though the error says "Music is crashing," it almost always means is your app allow to access the Music app's UI.

The app is requires to explicitly declare that app needs access to the user's Apple Music / Media Library. If you don't include this key, the system will instantly kill the process trying to access it, resulting in the exact crash log you posted I think, have not see it forever, so I’m digging into my small brain.

Do you have in your info.plist the NSAppleMusicUsageDescription.

https://developer.apple.com/documentation/bundleresources/information-property-list/nsapplemusicusagedescription

Are you testing in the simulator? The iOS Simulator does not have the Apple Music app installed. I believe that could cause the issue? You must test MPMediaPickerController on a physical iOS device. If you are testing on a physical device and the user has deleted the "Music" app, I think that could also be the issue?

To prevent your app from crashing, you should explicitly request/check for Media Library authorization before presenting the picker.

Albert Pascual
  Worldwide Developer Relations.

Crash while presenting a media picker for Music
 
 
Q