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?
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
}
This question looks like a dup of: Is there a specific Xcode compiler flag that gets set when compiling for iPad?
You can do a quick check of which device you are on with:
[[UIDevice currentDevice] model];
精彩评论