Iphone SDK 测定设备(iPhone、 iPod Touch)

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

转载于:https://stackoverflow.com/questions/448162/determine-device-iphone-ipod-touch-with-iphone-sdk

csdnceshi71
Memor.の There are some pretty good solutions in the thread that contains this answer
3 年多之前 回复
csdnceshi75
衫裤跑路 Nope, they're documented. developer.apple.com/iphone/library/documentation/System/…
接近 11 年之前 回复
weixin_41568184
叼花硬汉 Is the UIDeviceHardware class using undocumented and so forbidden libraries?
接近 11 年之前 回复

27个回答

You can use the UIDevice class like this:

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])
    // it's an iPhone
weixin_41568131
10.24 Works great. I would only change to [deviceType containsString:@"iPhone"] so that it works in Simulator as well.
接近 5 年之前 回复
csdnceshi59
ℙℕℤℝ UI_USER_INTERFACE_IDIOM() is safe to use in apps that still support iOS < 3.2: it is a macro in UIDevice.h specifically written to default to UIUserInterfaceIdiomPhone on such older iOS versions.
大约 8 年之前 回复
weixin_41568126
乱世@小熊 may I suggest unmarking this as the best answer and flagging the one that follows, with the full UIDeviceHardware class code, as the best, please?
9 年多之前 回复
csdnceshi55
~Onlooker i dont think that is supported in 3.0 OS
接近 10 年之前 回复
csdnceshi58
Didn"t forge i agree with you.. you have have written a answer very long back that is nice work .... i told instead of using model you can use UIUserInterfaceIdiomPad which is given for that purpose only...
大约 10 年之前 回复
weixin_41568183
零零乙 The code in that link is a compile-time check for Universal apps (apps which are compiled separately for iPhone and iPad). This code is a runtime check. Also keep in mind that this question and answer were written long before the iPad ever existed.
大约 10 年之前 回复

Please feel free to use this class (gist @ github)

CODE REMOVED AND RELOCATED TO

https://gist.github.com/1323251

UPDATE (01/14/11)

Obviously, this code is a bit out of date by now, but it can certainly be updated using the code on this thread provided by Brian Robbins which includes similar code with updated models. Thanks for the support on this thread.

csdnceshi53
Lotus@ It's nice that the latest code is available in gist, but the original code should not have been removed. Users shouldn't have to go to an external site to find the complete answer. You should leave the code here, but link to gist with advice that an updated version may be available there.
大约 5 年之前 回复
csdnceshi62
csdnceshi62 I find theiphonewiki.com/wiki/Models quite good as a source of new hardware identifiers
7 年多之前 回复
csdnceshi77
狐狸.fox See Updated Branch of the Gist. It covers iPad 3: gist.github.com/2238516
8 年多之前 回复
csdnceshi77
狐狸.fox This gist is not being actively edited. You will need to update is as you see fit. I have to assume the iPad3 would be iPad3,1 for the first revision.
8 年多之前 回复
weixin_41568126
乱世@小熊 Just checked the gist and I don't think the new iPad (iPad 3) has been included as yet. Anyone know the codes for it?
8 年多之前 回复
csdnceshi77
狐狸.fox Done. Good idea.
接近 9 年之前 回复
weixin_41568134
MAO-EYE My suggestion is to add this pretty simple piece of code as a gist in github, or as a very small open source project. Since Stack Overflow is not suited for this kind of development ;)
接近 9 年之前 回复
weixin_41568126
乱世@小熊 What about the next generation iPod? Isn't there a new model out?
接近 9 年之前 回复
weixin_41568126
乱世@小熊 - Tested it out. It works, edited the post to reflect your suggestion.
接近 9 年之前 回复
csdnceshi76
斗士狗 - Added. It's "iPhone4,1"
接近 9 年之前 回复
csdnceshi51
旧行李 : Some news about the iPhone 4S ?
接近 9 年之前 回复
weixin_41568134
MAO-EYE This line : NSString *platform = [NSString stringWithCString:machine]; , should be NSString *platform = [NSString stringWithUTF8String:machine]; since stringWithCString is deprecated.
大约 9 年之前 回复
csdnceshi77
狐狸.fox Thanks Jay. This code has already been updated several times since it was first posted. First post was Oct 2009, and you can see its been updated as far as Verizon iPhone 4
大约 9 年之前 回复
csdnceshi77
狐狸.fox Wow - way to totally re-write my code. That isn't the purpose of editing, so I rolled it back. Edits should never change the content of a post, just its readability. Read More: meta.stackexchange.com/questions/80513/…
9 年多之前 回复
csdnceshi51
旧行李 : I've edited your answer : made the class as a UIDevice category for easiest use, added an enum return type, updated the deprecated call, put the base platform call as private for internal use only. Of course, I've tested that new code to ensure it's still fully functionnal.
9 年多之前 回复
csdnceshi57
perhaps? when I run this... UIDeviceHardware *h=[[UIDeviceHardware alloc] init]; [self setDeviceModel:[h platformString]]; [h release]; i receive a warning "Method 'setDevice:' not found" I added this to my header file #import "UIDeviceHardware.h" #include <sys/types.h> #include <sys/sysctl.h>
9 年多之前 回复
csdnceshi78
程序go I made an update to add iPhone 3,3 which is the model id in my Verizon iPhone 4. And I can find no reference to iPhone 3,2 anywhere except in pre-Verizon web postings; not sure that device exists...
9 年多之前 回复
csdnceshi76
斗士狗 - I'm not seeing any iPhone3.2 in our access logs. I am, however, seeing a large number of iPhone3.3 devices. About 4% of our accesses come from that. Guessing that's the Verizon phone
9 年多之前 回复
csdnceshi77
狐狸.fox Every time? You should be able to call this class/method once and obtain the correct value for use throughout the app. Not sure what your objection is, exactly.
9 年多之前 回复
csdnceshi63
elliott.david It think that there's a need to cache those values to not call twenty or more if-then-else every time.
9 年多之前 回复
csdnceshi77
狐狸.fox Updated post. Thanks.
9 年多之前 回复
weixin_41568127
?yb? Just added one missing line to Brian Robbins's answer, for the Verizon iPhone 4. Its hardware code should be iPhone3,2.
9 年多之前 回复
weixin_41568131
10.24 one thing i don't get is why you're not defining those as class methods, since you're not maintaining any state. Simply replace - (NSString *) by + (NSString *) and you'll save yourself an alloc / release when using it.
9 年多之前 回复
weixin_41568184
叼花硬汉 The code does not break, it simply returns a slightly less pretty string. Do you have a better method for getting the device model? And by model, I mean more than just "iPhone" or "iPod", but the specific version.
9 年多之前 回复
csdnceshi52
妄徒之命 -1 This is totally unreliable. As soon as a new device comes out, this code breaks. Also, parsing the output of sysctl is a stupid idea, especially for something as simple as getting the device model.
9 年多之前 回复
csdnceshi77
狐狸.fox I honestly couldn't tell you. I have not done any iPad development.
10 年多之前 回复
csdnceshi61
derek5. Does this work with iPad as well? I know there would need to be another if statement, but the question is, does the iPad store it's information the same way? (IE: if ([platformisEqualToString:@"ipad1,1"]) return @"iPad 1G"; ) Thanks for this!
10 年多之前 回复

This is an update for UIDeviceHardware.m from the answer above.

- (NSString *)platformString
{
    NSString *platform = [self platform];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPod7,1"])      return @"iPod Touch 6G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad mini 4 (WiFi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad mini 4 (Cellular)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7 inch (Cellular)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9 inch (Cellular)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}
weixin_41568110
七度&光 what happened to iPad Air 2?
大约 5 年之前 回复
weixin_41568208
北城已荒凉 My app which uses uname function recently got rejected by app review for non-public API usage but they just didn't name any API in the comment. Is uname safe recently?
大约 5 年之前 回复
weixin_41568174
from.. FYI - it appears that an updated list is kept here - theiphonewiki.com/wiki/Models
接近 6 年之前 回复
weixin_41568126
乱世@小熊 Please update for iPhone 6/6 Plus. 'iPhone 6 Plus' = 'iPhone7,1', 'iPhone 6' = 'iPhone7,2'.
接近 6 年之前 回复
weixin_41568131
10.24 Updated list for iPhone 6 and 6 plus is here: stackoverflow.com/a/19584526/1374512
大约 6 年之前 回复
csdnceshi52
妄徒之命 The name for a cascaded NSString comparison yielding NSString is NSDictionary.The name for defining one without code dependencies is .plist.
6 年多之前 回复
csdnceshi69
YaoRaoLov Just updated platform strings for iPad mini 2G
接近 7 年之前 回复
csdnceshi78
程序go Could you update this for the new iPad air if possible?
接近 7 年之前 回复
weixin_41568134
MAO-EYE is there a better way to store this bunch of if statements that it does not have to be in the code directly? (for example in an plist file or something like that)
大约 7 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 I've updated for latest platforms using this link
7 年多之前 回复
csdnceshi74
7*4 Please update the value of "platform" for iPhone 5....
接近 8 年之前 回复
csdnceshi50
三生石@ Why the name iPad-3G? Is that the official name of the new iPad? I think iPad 4G or iPad 3 makes more sense.
8 年多之前 回复
csdnceshi71
Memor.の I've seen a good number of iPad3,2 devices in the wild – they're showing up with roughly 1/3 the frequency of the iPad3,3 devices.
8 年多之前 回复
csdnceshi70
笑故挽风 Yes, I rolled back that change
8 年多之前 回复
csdnceshi77
狐狸.fox Sorry, won't this function result in an infinite loop due to the first line? Who is 'self' here?
8 年多之前 回复

Just adding the iPhone 4S device code to this thread...

The iPhone 4S will return the string @"iPhone4,1".

csdnceshi62
csdnceshi62 I've added this to the most "upped" post
8 年多之前 回复

More usable

#include <sys/types.h>
#include <sys/sysctl.h>

@interface UIDevice(Hardware)

- (NSString *) platform;

- (BOOL)hasRetinaDisplay;

- (BOOL)hasMultitasking;

- (BOOL)hasCamera;

@end

@implementation UIDevice(Hardware)

- (NSString *) platform{
    int mib[2];
size_t len;
char *machine;

mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);

    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
return platform;
}

- (BOOL)hasRetinaDisplay {
    NSString *platform = [self platform];
    BOOL ret = YES;
    if ([platform isEqualToString:@"iPhone1,1"]) {
        ret = NO;
    }
    else
        if ([platform isEqualToString:@"iPhone1,2"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPhone2,1"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPod1,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod2,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod3,1"])      ret = NO;
    return ret;
}

- (BOOL)hasMultitasking {
    if ([self respondsToSelector:@selector(isMultitaskingSupported)]) {
        return [self isMultitaskingSupported];
    }
    return NO;
}

- (BOOL)hasCamera {
   BOOL ret = NO;
   // check camera availability
   return ret;
}

@end

you can reading properties with

NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO");
weixin_41568174
from.. Does not include a needed "hasVibration" feature.
7 年多之前 回复
csdnceshi77
狐狸.fox Instead of a complicated if/else tree, you can simply check whether [[UIScreen mainScreen] scale] is 2 or not (for iOS 5+).
8 年多之前 回复

How about this code, if new version was released, you will identifier with the last know device

- (NSString *)getModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);                              
    if ([sDeviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([sDeviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([sDeviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([sDeviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([sDeviceModel isEqual:@"iPhone3,1"]) return @"iPhone3GS";  //iPhone 4 - AT&T
    if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone3GS";  //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([sDeviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([sDeviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([sDeviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([sDeviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([sDeviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([sDeviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([sDeviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([sDeviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([sDeviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[sDeviceModel componentsSeparatedByString:@","] objectAtIndex:0];

//If a newer version exist
    if ([aux rangeOfString:@"iPhone"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4"
        if (version >= 4) return @"iPhone4s";

    }
    if ([aux rangeOfString:@"iPod"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version >=4) return @"iPod4thGen";
    }
    if ([aux rangeOfString:@"iPad"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version ==1) return @"iPad3G";
        if (version >=2) return @"iPad2";
    }
    //If none was found, send the original string
    return sDeviceModel;
}
weixin_41568134
MAO-EYE How to identify iPhone 5/iPod 5? is @"iPhone5,1" @"iPod5,1" the correct way to use?
接近 8 年之前 回复
csdnceshi64
游.程 What do you suggest? You have to imagine if a new device was released, you can't change the code, and for me it cause a bug.
接近 9 年之前 回复
csdnceshi68
local-host I don't like what you do. It returns bad references for unknown devices. Good idea, but bad implementation.
接近 9 年之前 回复
#import <sys/utsname.h> 

NSString *iPhone = [self iPhoneVersion]; 

-(NSString*)iPhoneVersion
{
   NSString *platform;
   struct utsname systemInfo;
   uname(&systemInfo);
   platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

   if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
   if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
   if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
   if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
   if ([platform isEqualToString:@"iPhone3,3"])    return @"VerizoniPhone 4";
   if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
   if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
   if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
   if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
   if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
   if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
   if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
   if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
   if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
   if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
   if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
   if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
   if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
   if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
   if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
   if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
   if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
   if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
   if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
   if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
   if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
   if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
   if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
   if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
   if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
   if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
   if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air";
   if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini 2G (WiFi)";
   if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini 2G (Cellular)";
   if ([platform isEqualToString:@"iPad4,6"])      return @"iPad Mini 2G";
   if ([platform isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (WiFi)";
   if ([platform isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
   if ([platform isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
   if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
   if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
   if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
   if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3";
   if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3 (2013)";
   if ([platform isEqualToString:@"i386"])         return @"Simulator";
   if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
   return platform;
}

For simple comparison I always like macro:

#define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

It's simple and easy to use.

Latest update - September 2017 Added iPhone X, iPhone 8, iPhone 8 Plus, Apple Watch Series 3, Apple TV 4K

Previous update - June 2017: Added iPad Pro 10,5" iPad Pro 12,9" 2nd Gen


Swift

(Tested on Swift 4, Swift 3)

The function below returns a string with current device name.

func userDeviceName() -> String {
    let platform: String = {
        var size = 0
        sysctlbyname("hw.machine", nil, &size, nil, 0)
        var machine = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname("hw.machine", &machine, &size, nil, 0)
        return String(cString: machine)
    }()

    //iPhone
    if platform == "iPhone1,1"        { return "iPhone (1st generation)" }
    else if platform == "iPhone1,2"   { return "iPhone 3G" }
    else if platform == "iPhone2,1"   { return "iPhone 3GS" }
    else if platform == "iPhone3,1"   { return "iPhone 4 (GSM)" }
    else if platform == "iPhone3,2"   { return "iPhone 4 (GSM, 2nd revision)" }
    else if platform == "iPhone3,3"   { return "iPhone 4 (CDMA)" }
    else if platform == "iPhone4,1"   { return "iPhone 4S" }
    else if platform == "iPhone5,1"   { return "iPhone 5 (GSM)" }
    else if platform == "iPhone5,2"   { return "iPhone 5 (GSM+CDMA)" }
    else if platform == "iPhone5,3"   { return "iPhone 5c (GSM)" }
    else if platform == "iPhone5,4"   { return "iPhone 5c (GSM+CDMA)" }
    else if platform == "iPhone6,1"   { return "iPhone 5s (GSM)" }
    else if platform == "iPhone6,2"   { return "iPhone 5s (GSM+CDMA)" }
    else if platform == "iPhone7,2"   { return "iPhone 6" }
    else if platform == "iPhone7,1"   { return "iPhone 6 Plus" }
    else if platform == "iPhone8,1"   { return "iPhone 6s" }
    else if platform == "iPhone8,2"   { return "iPhone 6s Plus" }
    else if platform == "iPhone8,4"   { return "iPhone SE" }
    else if platform == "iPhone9,1"   { return "iPhone 7 (GSM+CDMA)" }
    else if platform == "iPhone9,3"   { return "iPhone 7 (GSM)" }
    else if platform == "iPhone9,2"   { return "iPhone 7 Plus (GSM+CDMA)" }
    else if platform == "iPhone9,4"   { return "iPhone 7 Plus (GSM)" }
    else if platform == "iPhone10,1"   { return "iPhone 8 (GSM+CDMA)" }
    else if platform == "iPhone10,4"   { return "iPhone 8 (GSM)" }
    else if platform == "iPhone10,2"   { return "iPhone 8 Plus (GSM+CDMA)" }
    else if platform == "iPhone10,5"   { return "iPhone 8 Plus (GSM)" }
    else if platform == "iPhone10,3"   { return "iPhone X (GSM+CDMA)" }
    else if platform == "iPhone10,6"   { return "iPhone X (GSM)" }

    //iPod Touch
    else if platform == "iPod1,1"     { return "iPod Touch 1G" }
    else if platform == "iPod2,1"     { return "iPod Touch 2G" }
    else if platform == "iPod3,1"     { return "iPod Touch 3G" }
    else if platform == "iPod4,1"     { return "iPod Touch 4G" }
    else if platform == "iPod5,1"     { return "iPod Touch 5G" }
    else if platform == "iPod7,1"     { return "iPod Touch 6G" }

    //iPad
    else if platform == "iPad1,1"     { return "iPad (1st generation)" }
    else if platform == "iPad2,1"     { return "iPad 2 (Wi-Fi)" }
    else if platform == "iPad2,2"     { return "iPad 2 (GSM)" }
    else if platform == "iPad2,3"     { return "iPad 2 (CDMA)" }
    else if platform == "iPad2,4"     { return "iPad 2 (Wi-Fi, Mid 2012)" }
    else if platform == "iPad3,1"     { return "iPad (3rd generation) (Wi-Fi)" }
    else if platform == "iPad3,2"     { return "iPad (3rd generation) (GSM+CDMA)" }
    else if platform == "iPad3,3"     { return "iPad (3rd generation) (GSM)" }
    else if platform == "iPad3,4"     { return "iPad (4th generation) (Wi-Fi)"}
    else if platform == "iPad3,5"     { return "iPad (4th generation) (GSM)" }
    else if platform == "iPad3,6"     { return "iPad (4th generation) (GSM+CDMA)" }
    else if platform == "iPad6,11"    { return "iPad (5th generation) (Wi-Fi)" }
    else if platform == "iPad6,12"    { return "iPad (5th generation) (Cellular)" }

    //iPad Mini
    else if platform == "iPad2,5"     { return "iPad Mini (Wi-Fi)" }
    else if platform == "iPad2,6"     { return "iPad Mini (GSM)" }
    else if platform == "iPad2,7"     { return "iPad Mini (GSM+CDMA)" }
    else if platform == "iPad4,4"     { return "iPad Mini 2 (Wi-Fi)" }
    else if platform == "iPad4,5"     { return "iPad Mini 2 (Cellular)" }
    else if platform == "iPad4,6"     { return "iPad Mini 2 (China)" }
    else if platform == "iPad4,7"     { return "iPad Mini 3 (Wi-Fi)" }
    else if platform == "iPad4,8"     { return "iPad Mini 3 (Cellular)" }
    else if platform == "iPad4,9"     { return "iPad Mini 3 (China)" }
    else if platform == "iPad5,1"     { return "iPad Mini 4 (Wi-Fi)" }
    else if platform == "iPad5,2"     { return "iPad Mini 4 (Cellular)" }

    //iPad Air
    else if platform == "iPad4,1"     { return "iPad Air (Wi-Fi)" }
    else if platform == "iPad4,2"     { return "iPad Air (Cellular)" }
    else if platform == "iPad4,3"     { return "iPad Air (China)" }
    else if platform == "iPad5,3"     { return "iPad Air 2 (Wi-Fi)" }
    else if platform == "iPad5,4"     { return "iPad Air 2 (Cellular)" }

    //iPad Pro
    else if platform == "iPad6,3"     { return "iPad Pro 9.7\" (Wi-Fi)" }
    else if platform == "iPad6,4"     { return "iPad Pro 9.7\" (Cellular)" }
    else if platform == "iPad6,7"     { return "iPad Pro 12.9\" (Wi-Fi)" }
    else if platform == "iPad6,8"     { return "iPad Pro 12.9\" (Cellular)" }
    else if platform == "iPad7,1"     { return "iPad Pro 12.9\" (2nd generation) (Wi-Fi)" }
    else if platform == "iPad7,2"     { return "iPad Pro 12.9\" (2nd generation) (Cellular)" }
    else if platform == "iPad7,3"     { return "iPad Pro 10.5\" (Wi-Fi)" }
    else if platform == "iPad7,4"     { return "iPad Pro 10.5\" (Cellular)" }

    //Apple TV
    else if platform == "AppleTV2,1"  { return "Apple TV 2G" }
    else if platform == "AppleTV3,1"  { return "Apple TV 3" }
    else if platform == "AppleTV3,2"  { return "Apple TV 3 (2013)" }
    else if platform == "AppleTV5,3"  { return "Apple TV 4" }
    else if platform == "AppleTV6,2"  { return "Apple TV 4K" }

    //Apple Watch
    else if platform == "Watch1,1"    { return "Apple Watch (1st generation) (38mm)" }
    else if platform == "Watch1,2"    { return "Apple Watch (1st generation) (42mm)" }
    else if platform == "Watch2,6"    { return "Apple Watch Series 1 (38mm)" }
    else if platform == "Watch2,7"    { return "Apple Watch Series 1 (42mm)" }
    else if platform == "Watch2,3"    { return "Apple Watch Series 2 (38mm)" }
    else if platform == "Watch2,4"    { return "Apple Watch Series 2 (42mm)" }
    else if platform == "Watch3,1"    { return "Apple Watch Series 3 (38mm Cellular)" }
    else if platform == "Watch3,2"    { return "Apple Watch Series 3 (42mm Cellular)" }
    else if platform == "Watch3,3"    { return "Apple Watch Series 3 (38mm)" }
    else if platform == "Watch3,4"    { return "Apple Watch Series 3 (42mm)" }

    //Simulator
    else if platform == "i386"        { return "Simulator" }
    else if platform == "x86_64"      { return "Simulator"}

    return platform
}

You can test it using following code:

print("Current device is: ", self.userDeviceName())

Objective-C

I also added new devices to the Objective-C code

Don't forget to import sys/sysctl.h

 #import <sys/sysctl.h>  


- (NSString *) userDeviceName {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];

    free(machine);

    //iPhone
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone (1st generation)";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4 (GSM)";
    if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 (GSM, 2nd revision)";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (Verizon)";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"])    return @"iPhone 7 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone9,3"])    return @"iPhone 7 (GSM)";
    if ([platform isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus (GSM)";
    if ([platform isEqualToString:@"iPhone10,1"])    return @"iPhone 8 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,4"])    return @"iPhone 8 (GSM)";
    if ([platform isEqualToString:@"iPhone10,2"])    return @"iPhone 8 Plus (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,5"])    return @"iPhone 8 Plus (GSM)";
    if ([platform isEqualToString:@"iPhone10,3"])    return @"iPhone X (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,6"])    return @"iPhone X (GSM)";

    //iPod Touch
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPod7,1"])      return @"iPod Touch 6G";

    //iPad
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad (1st generation)";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (Wi-Fi, Mid 2012)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (Wi-Fi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad (3rd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad (3rd generation) (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad (3rd generation) (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad (4th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad (4th generation) (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad (4th generation) (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air (China)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
    if ([platform isEqualToString:@"iPad4,6"])      return @"iPad Mini 2 (China)";
    if ([platform isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
    if ([platform isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (Cellular)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7\" (Cellular)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9\" (Cellular)";
    if ([platform isEqualToString:@"iPad6,11"])     return @"iPad (5th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,12"])     return @"iPad (5th generation) (Cellular)";
    if ([platform isEqualToString:@"iPad7,1"])      return @"iPad Pro 12.9\" (2nd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,2"])      return @"iPad Pro 12.9\" (2nd generation) (Cellular)";
    if ([platform isEqualToString:@"iPad7,3"])      return @"iPad Pro 10.5\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,4"])      return @"iPad Pro 10.5\" (Cellular)";

    //Apple TV
    if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
    if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3";
    if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3 (2013)";
    if ([platform isEqualToString:@"AppleTV5,3"])   return @"Apple TV 4";
    if ([platform isEqualToString:@"AppleTV6,2"])   return @"Apple TV 4K";

    //Apple Watch
    if ([platform isEqualToString:@"Watch1,1"])     return @"Apple Watch (1st generation) (38mm)";
    if ([platform isEqualToString:@"Watch1,2"])     return @"Apple Watch (1st generation) (42mm)";
    if ([platform isEqualToString:@"Watch2,6"])     return @"Apple Watch Series 1 (38mm)";
    if ([platform isEqualToString:@"Watch2,7"])     return @"Apple Watch Series 1 (42mm)";
    if ([platform isEqualToString:@"Watch2,3"])     return @"Apple Watch Series 2 (38mm)";
    if ([platform isEqualToString:@"Watch2,4"])     return @"Apple Watch Series 2 (42mm)";
    if ([platform isEqualToString:@"Watch3,1"])     return @"Apple Watch Series 3 (38mm Cellular)";
    if ([platform isEqualToString:@"Watch3,2"])     return @"Apple Watch Series 3 (42mm Cellular)";
    if ([platform isEqualToString:@"Watch3,3"])     return @"Apple Watch Series 3 (38mm)";
    if ([platform isEqualToString:@"Watch3,4"])     return @"Apple Watch Series 3 (42mm)";

    //Simulator
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}

You can test it using following code:

NSLog(@"Current device is: %@", [self userDeviceName]);
csdnceshi69
YaoRaoLov Check this github.com/aleemrazzaq/ARCompactDeviceInfo this has latest Device model, iPhone X, iPhone 8, 8 Plus and iPad 6th Generation 2018
2 年多之前 回复
csdnceshi56
lrony* Yes, this is correct. You can check here: theiphonewiki.com/wiki/Models
接近 4 年之前 回复
csdnceshi67
bug^君 Just to clarify... iPhone7,2 is the 6 but iPhone8,1 is the 6S? they reversed for the 6 and 6S?
接近 4 年之前 回复

I know an answer has been ticked already, but for future reference, you could always use the device screen size to figure out which device it is like so:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    CGSize result = [[UIScreen mainScreen] bounds].size;

    if (result.height == 480) {
        // 3.5 inch display - iPhone 4S and below
        NSLog(@"Device is an iPhone 4S or below");
    }

    else if (result.height == 568) {
        // 4 inch display - iPhone 5
        NSLog(@"Device is an iPhone 5/S/C");
    }

    else if (result.height == 667) {
        // 4.7 inch display - iPhone 6
        NSLog(@"Device is an iPhone 6");
    }

    else if (result.height == 736) {
        // 5.5 inch display - iPhone 6 Plus
        NSLog(@"Device is an iPhone 6 Plus");
    }
} 

else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
       // iPad 9.7 or 7.9 inch display.
       NSLog(@"Device is an iPad.");
}
csdnceshi77
狐狸.fox more easy and working code than others given here
大约 4 年之前 回复
csdnceshi71
Memor.の Yes of course!
大约 5 年之前 回复
csdnceshi52
妄徒之命 It is works even with autolayout?
大约 5 年之前 回复
共27条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问