0

UIImagePicker must be presented differently on iPhone and iPad. On the iPad, it throws an exception saying this:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

So I must add code to my universal app which is iPad specific. What is a safe way to do it so that the app won't crash on devices which lack a UIPopoverController?

Example:

popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        [popover presentPopoverFromRect:CGRectMake(100, 100.0, 0.0, 0.0) 
                                 inView:self.view
               permittedArrowDirections:UIPopoverArrowDirectionAny 
                               animated:YES];

Also, in case I need a UIPopoverController, I need an instance variable that holds it. How would I handle this?

Proud Member
  • 38,700
  • 43
  • 143
  • 225

3 Answers3

3

You can check to see if a class exists by doing this.

Class popoverClass = (NSClassFromString(@"UIPopoverController"));
if (popoverClass != nil) {
// you're on ipad
} else {
// you're on iphone/ipod touch
}
i300
  • 116
  • 2
2

This question looks like a dup of: Is there a specific Xcode compiler flag that gets set when compiling for iPad?

Community
  • 1
  • 1
Mark Pauley
  • 1,314
  • 11
  • 11
2

You can do a quick check of which device you are on with:

[[UIDevice currentDevice] model];
Dancreek
  • 9,387
  • 1
  • 29
  • 33
  • 1
    The `UI_USER_INTERFACE_IDIOM` macro in the UIDevice header is probably a more appropriate test. – Jonah May 24 '11 at 16:49
  • This is more straight to the point: if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) – Paulo Sep 24 '13 at 08:28