关于UI_USER_INTERFACE_IDIOM() & UIDevice.model

伯晨
2023-12-01

使用  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, NULL0);

    char *machine = malloc(size);

    sysctlbyname("hw.machine", machine, &size, NULL0);

    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 @"";

}

原文地址:http://blog.sina.com.cn/s/blog_890a737301014fim.html

 类似资料: