iOSiOS Developer

获取当前的iOS设备型号

2016-11-14  本文已影响48人  码痞
+ (NSString*)getDeviceInfo{

    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"5";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"5";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"5C";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"5C";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"5S";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"5S";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"6P";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"6S";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"6SP";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"SE";
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"7";
    if ([deviceString isEqualToString:@"iPhone9.2"])    return @"7P";
    //
    if ([deviceString isEqualToString:@"iPad3,1"])    return @"iPad3";
    if ([deviceString isEqualToString:@"iPad3,2"])    return @"iPad3";
    if ([deviceString isEqualToString:@"iPad3,3"])    return @"iPad3";

    //
    if ([deviceString isEqualToString:@"iPad3,4"])    return @"iPad4";
    if ([deviceString isEqualToString:@"iPad3,5"])    return @"iPad4";
    if ([deviceString isEqualToString:@"iPad3,6"])    return @"iPad4";

    //
    if ([deviceString isEqualToString:@"iPad4,1"])    return @"iPadAir";
    if ([deviceString isEqualToString:@"iPad4,1"])    return @"iPadAir";
    if ([deviceString isEqualToString:@"iPad4,1"])    return @"iPadAir";

    //
      return @"other";
}

Tips:

1.OC中switch-case 无法匹配字符串,故使用if,
swift 中建议使用switch-case 而不是if

2.进行屏幕匹配建议使用屏幕尺寸而不是设备型号

上一篇 下一篇

猜你喜欢

热点阅读