陈斌彬的技术博客

Stay foolish,stay hungry

关于UI_USER_INTERFACE_IDIOM()

使用 UI_USER_INTERFACE_IDIOM() 进行区分 (ios 3.2 >=) 无法区分iphone和ipod

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
       // <define your constants here>
    } else {
      //  <define your constants here>
    }

区分目前UI_USER_INTERFACE_IDIOM()有2个返回值
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad    

UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone    

使用 UIDevice.model 进行区分 (ios 2.0 >=)

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"]) {
    //iPhone
}
else if([deviceType isEqualToString:@"iPod touch"]) {
    //iPod Touch
}
else {
    //iPad
}

使用系统的一个函数sysctlbyname 来获取设备名称

- (NSString *) platformString
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);
    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:@"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:@"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:@"i386"])         return @"Simulator";
    return @"";
}