0

I have a universal app in Xcode. If the user is using an iPad the use image from library button works great. However if they use an iPhone the button doesn't work.

Here is the error I receive.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'

Was told this code would work. Does it go into the (IBAction) Code below?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { Add Popover code here } else {     
Add alternative for popover here }

- (IBAction) useCameraRoll: (id)sender
{

if ([self.popoverController isPopoverVisible]) {
    [self.popoverController dismissPopoverAnimated:YES];

} else {
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = YES;

        newMedia = NO;
    }
}
}

1 Answers1

0

This crash is occuring because you can't use UIPopOverControllers on iPhones, only on iPads.

[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiom Pad.'

Try something like this:

- (IBAction)useCameraRoll:(id)sender
{
    UIPopoverController *popoverController = [[UIPopoverController alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = YES;

        newMedia = NO;
    }
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        if (popoverController.isPopoverVisible) {
            [popoverController dismissPopoverAnimated:YES];
        }else{
                [popoverController setContentViewController:imagePicker];
                [popoverController setDelegate:self];
                [popoverController presentPopoverFromRect:CGRectMake(0, 0, 320, 320) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }else{
        [imagePicker presentViewController:imagePicker animated:YES completion:nil];
    }
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Ok so that code gives me a few warning and errors. Warnings are local declaration of popoverController hides instance variable. The errors are use of undeclared identifier image picker. – Niche' Ad Marketing Sep 06 '12 at 16:29
  • @Niche'AdMarketing I'm aware of these, the only reason I left them would be so you'd know you'd have to move some things around, like the declaration of "imagePicker". Since it is contained within the `isSourceTypeAvailable` "if" it cannot be used outside of it. declaring the imagePicker in your header file should silence this. – Mick MacCallum Sep 06 '12 at 16:30
  • Declaring imagePicker in the .h would it look like this? UIImageView *imagePicker; then @property (nonatomic, retain) IBOutlet UIImageView *imagePicker; – Niche' Ad Marketing Sep 06 '12 at 16:50
  • @Niche'AdMarketing You don't need the `@property`, and no this isn't a UIImageView, it is `UIImagePickerController *imagePicker;` – Mick MacCallum Sep 06 '12 at 16:51