简体   繁体   中英

How do I determine whether the current device is an iPhone or iPad?

In my universal app I need to check if the current device is an iPad or iPhone. How can I do this programmatically? I plan to put the code in my viewDidLoad.

check if UISplitViewController class available on the platform, if so make sure it is iPad using Apple's macro (notice that UIUserInterfaceIdiomPad constant is available only on iOS 3.2 and up).

if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        //currentDeviceType = iPad;
    }
    else {
        //currentDeviceType = iPhone;
    }

I use this simple function in all my apps:

#import "isPad.h"
BOOL isPad () {
    static BOOL isPad;
    static BOOL used = NO;  
    if (used)
        return isPad;
    used = YES;
    NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
    isPad = range.location != NSNotFound;
    return isPad;
}
try this.. this will help you.   

 NSString *deviceType = [UIDevice currentDevice].model;


        if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPhone"]||[deviceType isEqualToString:@"iPad"]){
        }
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // Statements
}

else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
   // Statements
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM