views:

46

answers:

2

I've found this code, here:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        str = [NSString stringWithString:@"Running as an iPad application"];
    } else {
        str = [NSString stringWithString:
                  @"Running as an iPhone/iPod touch application"];
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Platform"
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];   

How safe is this check? Does Apple actually recommend doing this? Or can it happen that it won't detect an iPad as iPad, or iPhone as iPhone?

+3  A: 

It should be safe enough, it's well-documented by Apple.

That is just shorthand for the following code:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// etc

It could conceivably fail if you tried to run this on anything less than iOS 3.2 (as it was only introduced then), but this might not be an issue for you.

Sedate Alien
Thanks a lot! That solves like trillions of problems for me...
openfrog
This actually doesn't fail when run on earlier OSs. On a pre-3.2 OS, the expression will evaluate to 0, which is not equal to the UIUserInterfaceIdiomPad value of 1, so it will return the correct result.
Brad Larson
A: 

If you want to run your code below IOS 3.2 you can use below code

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {
               // Code for iPad
            }
            else
            {
                // Code for iPhone or iPod (Will run if IOS is more than 3.2 like 4.0 and above
            }
        #else 
            // Code for iPhone or iPod (below IOS 3.2)
        #endif  
Avinash
Note that this is for compiling your code on an earlier OS, not running your application on that OS. These compiler-time checks will do nothing to alter runtime behavior.
Brad Larson