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.

I have: <key>NSAppleMusicUsageDescription</key> <string>The application may use the tunes in your library as a backdrop for your meditations.</string> and testing on the device. How do I request, check for Media Library authorization, considering this feature has worked for two years years without any problems, until the latest issue?

Thank. Like my previous post, is the app crashing on the simulator or is crashing on a device? I don't think there is any issue with NSAppleMusicUsageDescription can you reproduce the issue with a simple focused project with that entitlement?

Albert Pascual
  Worldwide Developer Relations.

And this is the full error display:

Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process 63584 does not exist" UserInfo={NSLocalizedFailureReason=Specified target process 63584 does not exist}> [u 31CF99CC-85C8-4FDF-9567-8ED0FF31C74F:m (null)] [com.apple.Music.MediaPicker(3.1)] could not acquire startup assertion Plugin <id<PKPlugIn>: 0x11db6f640; core = <[u 31CF99CC-85C8-4FDF-9567-8ED0FF31C74F] [com.apple.Music.MediaPicker(3.1)],[d 239CAB04-AF5B-44B7-9644-19066F49B18E] [/private/var/containers/Bundle/Application/641E7C8C-5A59-41D0-802C-60006EE341AE/Music.app/PlugIns/MediaPicker.appex]>, instance = [(null)], state = 3, useCount = 0> must have pid! Extension request will fail Failed to acquire assertion for plugin: <id<PKPlugIn>: 0x11db6f640; core = <[u 31CF99CC-85C8-4FDF-9567-8ED0FF31C74F] [com.apple.Music.MediaPicker(3.1)],[d 239CAB04-AF5B-44B7-9644-19066F49B18E] [/private/var/containers/Bundle/Application/641E7C8C-5A59-41D0-802C-60006EE341AE/Music.app/PlugIns/MediaPicker.appex]>, instance = [(null)], state = 3, useCount = 0> pid: 0 Unable to acquire process assertion in beginUsing: with plugin identifier: com.apple.Music.MediaPicker, killing plugin begin extension request <EXExtensionRequest: 0x11db68a10> Request PK UUID: CB10114C-0A82-4781-9B26-17A754A333B8 with item count 0 complete with error: Error Domain=PlugInKit Code=4 "could not acquire startup assertion" UserInfo={NSLocalizedDescription=could not acquire startup assertion} PlugInKit error in beginUsing: with plugin identifier: com.apple.Music.MediaPicker, killing plugin *** 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 0x103470544 0x103489ccc 0x1034ab748 0x103480204 0x103480144 0x186957a64 0x1868e5288 0x1868e41d0 0x22bde7498 0x18c5a5ca0 0x18c510254 0x18c71ce90 0x103967154 0x1039670c4 0x103967244 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.' *** First throw call stack: (0x1869cac70 0x183499224 0x1844f9f50 0x1a6c6a060 0x18d45518c 0x18d4cd410 0x103470544 0x103489ccc 0x1034ab748 0x103480204 0x103480144 0x186957a64 0x1868e5288 0x1868e41d0 0x22bde7498 0x18c5a5ca0 0x18c510254 0x18c71ce90 0x103967154 0x1039670c4 0x103967244 0x1834f1c1c) terminating due to uncaught exception of type NSException warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.

Thanks!

On device? I do not see any bugs on that part, and I would really encourage you to create a simple focused project so you can show the crash on a device after submitting a bug that I think will be the best approach in this case.

Once you open the bug report, please post the FB number here for my reference.

If you have any questions about filing a bug report, take a look at Bug Reporting: How and Why?

Albert Pascual
  Worldwide Developer Relations.

Still when I try to create a new project, I'm presented with a swiftUI framework I do not know what to make with. Is not any longer possible to start a new project with storyboard and using plain Swift code?

I long ago posted a bug report, code is FB22200912.

Thanks @fbartolom That bug is from March 11th, just last week. I'll post a link to this thread for the team. Please use Feedback Assistant to communicate with that team and hopefully they'll have a resolution for you soon.

For more details on when you'll see updates to your report, please see What to expect after submission.

Albert Pascual
  Worldwide Developer Relations.

I produced a simple project with the same configuration and authorizations and it works regularly. I'm quite puzzled. I do not know how I may send it to you.

@fbartolom you can upload to the bug using Feedback Assistant or you can put a link here and I can download it for you and added into the bug. Either way will work.

Albert Pascual
  Worldwide Developer Relations.

Very strangely on the test app I was requested to allow the usage of the music repository and now even the main app allows to use the music repository. I wonder how may I request the same thing on the main app, especially given I'am always reported as authorized.

If your test app and main app share the same Bundle ID, granting permission in the test app automatically grants it in the main app. Furthermore, once an app is authorized, the system will never show the prompt again, which is why your main app is always reporting as "authorized."

Before you can request access, your main app must have the privacy string in its Info.plist. If you call the authorization code without this, your app will crash. I would check on your workflow.

Since your app is currently stuck in the .authorized state, calling requestAuthorization will just instantly return .authorized without showing the UI. To test the prompt, you need to revoke the permission.

When you install that app into a new device do you have that issue?

Albert Pascual
  Worldwide Developer Relations.

Crash while presenting a media picker for Music
 
 
Q