Search code examples
objective-cios-simulatorios4weak-linking

Problem with detection of UIPopoverController class availability when running on iPhone simulator


I work on an universal app that uses UIPopoverController for the iPad version. (Both the base SDK and the deployment targets are iOS 4.3)

When I use the iPhone simulator (version 4.3) to test conditional code paths for iPad and iPhone neither the weak linking nor the NSClassFromString(@"UIPopoverController") approach give the expected behavior.

When testing weak linking of the UIKit framework I make sure to user the LLVM 2.1 compiler and make the UIKit framework 'Optional' in 'Target->Build Phases->Link Binary with Libraries'. (As I understand is the way to do it in Xcode 4.1).

Running the code

if ([UIPopoverController class]) { 
...
_popover = [[UIPopoverController alloc] initWithContentViewController:controller];
...
}

on the iPhone simulator crashes the application indicating that the [UIPopoverController class] does not return nil. Replacing [UIPopoverController class] with NSClassFromString(@"UIPopoverController") above, results in the same crash.

Does anyone know how to go about to make these conditional checks work when running on the simulator?


Solution

  • Try determining whether or not the code is running on an iPad.
    If it does, you can safely use UIPopoverController.

    - (BOOL)isPad
    {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)])
        {
            return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
        }
        else
        {
            return NO; // all iPad OS's implement -userInterfaceIdiom
        }
    }