Runtime error when using MPMediaPickerController in iOS Simulator
Asked Answered
H

3

12

The following happens when I try to run an app using the MPMediaPickerController on the iOS Simulator.

2012-05-28 22:26:42.416 My App[48426:11f03] Could not load source: 3
2012-05-28 22:26:42.418 My App[48426:11f03] *** Assertion failure in -[MPMediaPickerController loadView], /SourceCache/MediaPlayer_Sim/MobileMusicPlayer-1391.72/SDK/MPMediaPickerController.m:86
2012-05-28 22:26:42.419 My App[48426:11f03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to load iPodUI.framework'

Is this some problem in my App/Xcode/iOS Simulator, or does the iOS Simulator simply not support the MPMediaPickerController? If not, any alternatives, besides running it on a physical device?

Hephzibah answered 29/5, 2012 at 2:30 Comment(1)
This is now possible, see my answer below for details.Gensmer
B
15

MPMediaPickerController does not work in the Simulator. Apple notes this in the "iPod Library Access Programming Guide" under "Hello Music Player". The note says:

Note: To follow these steps you’ll need a provisioned device because the Simulator has no access to a device’s iPod library.

To prevent the assertion you can always check if you can access the do this in your code (code bellow uses ARC and iOS SDK 5.0).

MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];

[picker setDelegate:self];
[picker setAllowsPickingMultipleItems:YES];
[picker setPrompt:NSLocalizedString(@"Add songs to play","Prompt in media item picker")];

@try {
    [picker loadView]; // Will throw an exception in iOS simulator
    [self presentViewController:picker animated:YES completion:nil];
}
@catch (NSException *exception) {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Oops!",@"Error title")
                                message:NSLocalizedString(@"The music library is not available.",@"Error message when MPMediaPickerController fails to load") 
                               delegate:nil 
                      cancelButtonTitle:@"OK" 
                      otherButtonTitles:nil] show];
}
Bandit answered 31/5, 2012 at 20:17 Comment(2)
I was wondering if adding the try catch code is a requirement for apps to be successfully added to the app store? I don't intent to add this code if it is not a requirement.Awildaawkward
While correct at the time, the current iOS Simulators allow rudimentary support for accessing the music library; see my answer for details.Gensmer
A
2

Also (if using storyboard) you can try it:

- (IBAction)showPicker:(id)sender
{
#if TARGET_IPHONE_SIMULATOR
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"playerTest"
                                                    message:@"Media picker didn't work in simulator, please run this app on device"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
#else
    [self performSegueWithIdentifier:@"ShowPickerViewSegue" sender:self];
#endif
}
Abeu answered 29/11, 2012 at 2:56 Comment(0)
G
0

MPMediaPickerController now works in the iOS Simulator without any additional code changes (at least as of iOS 8, possibly earlier). Here is a project that can demonstrate it: GVMusicPlayerController.

You will have to prepare the music library in the Simulator by copying the necessary files from an actual device, most importantly the MediaLibrary.sqlitedb database files. If you want to play the files and view artwork, you'll also have to copy the iTunes_Control/Music, Purchases and Artwork folders (found in /var/mobile/Media/). See this question for further details: Can i access iPod Library on simulator?.

Gensmer answered 11/10, 2016 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.