admin管理员组

文章数量:829151

ios 区分iphone ipod ipad的方法及获取设备名称。

coding如何区分iphone ipod & ipad 的几种方法

UI_USER_INTERFACE_IDIOM
Returns the interface idiom supported by the current device.#define UI_USER_INTERFACE_IDIOM() \([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \[[UIDevice currentDevice] userInterfaceIdiom] : \UIUserInterfaceIdiomPhone)
Return Value
UIUserInterfaceIdiomPhone if the device is an iPhone or iPod touch or UIUserInterfaceIdiomPad if the device is an iPad.


1)使用  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    //ipad


UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone    //ipad


2)使用 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

    }


3)使用系统的一个函数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 @"";

}


本文标签: ios 区分iphone ipodipad的方法及获取设备名称