読者です 読者をやめる 読者になる 読者になる

ふるすたっくえんじにあっぽい人の日記

ASP .NET MVC (C#)、.NET Framework、iOS (Objective-c) アプリ、Androidアプリ (Java)、AWS、たまにLAMPとかプロジェクトマネジメントあたりのお話

【iOS/Objective-c】プラットフォーム情報取得3~Objective-c編~

Objective-c iOS

晒す。ただ、つかれた。

ここを参考にちょっと変えて、Objective-cで書いてある。
Apple iPhone Specs (All iPhone Models) @ EveryiPhone.com

ま、enumもplatformNameもクライアントで処理しなきゃいけないケースはあんまり思い浮かばない。

typedef NS_ENUM(NSUInteger, Platform) {
    PlatformUnkonwn = 0,
    
    // iPhone
    PlatformiPhone = 100,
    PlatformiPhone3G = 101,
    PlatformiPhone3GS = 102,
    PlatformiPhone4_GSM = 103,
    PlatformiPhone4_RevA = 104,
    PlatformiPhone4_CDMA = 105,
    PlatformiPhone4S = 106,
    PlatformiPhone5_GSM = 107,
    PlatformiPhone5_CDMA = 108,
    PlatformiPhone5c_GSM = 109,
    PlatformiPhone5c_CDMA = 110,
    PlatformiPhone5s_GSM = 111,
    PlatformiPhone5s_CDMA = 112,
    PlatformiPhone6Plus = 113,
    PlatformiPhone6 = 114,
    
    // iPod
    PlatformiPodTouch1G = 200,
    PlatformiPodTouch2G = 201,
    PlatformiPodTouch3G = 202,
    PlatformiPodTouch4G = 203,
    PlatformiPodTouch5G = 204,
    
    // iPad
    PlatformiPad_Wifi = 300,
    PlatformiPad2_WiFi = 301,
    PlatformiPad2_GSM = 302,
    PlatformiPad2_CDMA = 303,
    PlatformiPad2_RevA = 304,
    PlatformiPadMini_WiFi = 305,
    PlatformiPadMini_GSM = 306,
    PlatformiPadMini_CDMA = 307,
    PlatformiPad3_WiFi = 308,
    PlatformiPad3_GSM = 309,
    PlatformiPad3_CDMA = 310,
    PlatformiPad4_WiFi = 311,
    PlatformiPad4_GSM = 312,
    PlatformiPad4_CDMA = 313,
    PlatformiPadAir_WiFi = 314,
    PlatformiPadAir_Cellular = 315,
    PlatformiPadAir_China = 316,
    PlatformiPadMini2_WiFi = 317,
    PlatformiPadMini2_Cellular = 318,
    PlatformiPadMini2_China = 319,
    PlatformiPadMini3_WiFi = 320,
    PlatformiPadMini3_Cellular = 321,
    PlatformiPadMini3_China = 322,
    PlatformiPadAir2_WiFi = 323,
    PlatformiPadAir2_Cellular = 324,
    
    // AppleTV
    PlatformAppleTV1G = 401,
    PlatformAppleTV2G = 402,
    
    // Simulator
    Platformi386Simulator = 501,
    Platformx86_64Simulator = 502,
};

- (Platform)platform
{
    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);
    
    if ([platform isEqualToString:@"iPhone1,1"])
        return PlatformiPhone;
    else if ([platform isEqualToString:@"iPhone1,2"])
        return PlatformiPhone3G;
    else if ([platform isEqualToString:@"iPhone2,1"])
        return PlatformiPhone3GS;
    else if ([platform isEqualToString:@"iPhone3,1"])
        return PlatformiPhone4_GSM;
    else if ([platform isEqualToString:@"iPhone3,2"])
        return PlatformiPhone4_RevA;
    else if ([platform isEqualToString:@"iPhone3,3"])
        return PlatformiPhone4_CDMA;
    else if ([platform isEqualToString:@"iPhone4,1"])
        return PlatformiPhone4S;
    else if ([platform isEqualToString:@"iPhone5,1"])
        return PlatformiPhone5_GSM;
    else if ([platform isEqualToString:@"iPhone5,2"])
        return PlatformiPhone5_CDMA;
    else if ([platform isEqualToString:@"iPhone5,3"])
        return PlatformiPhone5c_GSM;
    else if ([platform isEqualToString:@"iPhone5,4"])
        return PlatformiPhone5c_CDMA;
    else if ([platform isEqualToString:@"iPhone6,1"])
        return PlatformiPhone5s_GSM;
    else if ([platform isEqualToString:@"iPhone6,2"])
        return PlatformiPhone5s_CDMA;
    else if ([platform isEqualToString:@"iPhone7,1"])
        return PlatformiPhone6Plus;
    else if ([platform isEqualToString:@"iPhone7,2"])
        return PlatformiPhone6;
    else if ([platform isEqualToString:@"iPod1,1"])
        return PlatformiPodTouch1G;
    else if ([platform isEqualToString:@"iPod2,1"])
        return PlatformiPodTouch2G;
    else if ([platform isEqualToString:@"iPod3,1"])
        return PlatformiPodTouch3G;
    else if ([platform isEqualToString:@"iPod4,1"])
        return PlatformiPodTouch4G;
    else if ([platform isEqualToString:@"iPod5,1"])
        return PlatformiPodTouch5G;
    else if ([platform isEqualToString:@"iPad1,1"])
        return PlatformiPad_Wifi;
    else if ([platform isEqualToString:@"iPad2,1"])
        return PlatformiPad2_WiFi;
    else if ([platform isEqualToString:@"iPad2,2"])
        return PlatformiPad2_GSM;
    else if ([platform isEqualToString:@"iPad2,3"])
        return PlatformiPad2_CDMA;
    else if ([platform isEqualToString:@"iPad2,4"])
        return PlatformiPad2_RevA;
    else if ([platform isEqualToString:@"iPad2,5"])
        return PlatformiPadMini_WiFi;
    else if ([platform isEqualToString:@"iPad2,6"])
        return PlatformiPadMini_GSM;
    else if ([platform isEqualToString:@"iPad2,7"])
        return PlatformiPadMini_CDMA;
    else if ([platform isEqualToString:@"iPad3,1"])
        return PlatformiPad3_WiFi;
    else if ([platform isEqualToString:@"iPad3,2"])
        return PlatformiPad3_GSM;
    else if ([platform isEqualToString:@"iPad3,3"])
        return PlatformiPad3_CDMA;
    else if ([platform isEqualToString:@"iPad3,4"])
        return PlatformiPad4_WiFi;
    else if ([platform isEqualToString:@"iPad3,5"])
        return PlatformiPad4_GSM;
    else if ([platform isEqualToString:@"iPad3,6"])
        return PlatformiPad4_CDMA;
    else if ([platform isEqualToString:@"iPad4,1"])
        return PlatformiPadAir_WiFi;
    else if ([platform isEqualToString:@"iPad4,2"])
        return PlatformiPadAir_Cellular;
    else if ([platform isEqualToString:@"iPad4,3"])
        return PlatformiPadAir_China;
    else if ([platform isEqualToString:@"iPad4,4"])
        return PlatformiPadMini2_WiFi;
    else if ([platform isEqualToString:@"iPad4,5"])
        return PlatformiPadMini2_Cellular;
    else if ([platform isEqualToString:@"iPad4,6"])
        return PlatformiPadMini2_China;
    else if ([platform isEqualToString:@"iPad4,7"])
        return PlatformiPadMini3_WiFi;
    else if ([platform isEqualToString:@"iPad4,8"])
        return PlatformiPadMini3_Cellular;
    else if ([platform isEqualToString:@"iPad4,9"])
        return PlatformiPadMini3_China;
    else if ([platform isEqualToString:@"iPad5,3"])
        return PlatformiPadAir2_WiFi;
    else if ([platform isEqualToString:@"iPad5,4"])
        return PlatformiPadAir2_Cellular;
    else if ([platform isEqualToString:@"AppleTV1,1"])
        return PlatformAppleTV1G;
    else if ([platform isEqualToString:@"AppleTV1,2"])
        return PlatformAppleTV2G;
    else if ([platform isEqualToString:@"i386"])
        return Platformi386Simulator;
    else if ([platform isEqualToString:@"x86_64"])
        return Platformx86_64Simulator;
    return PlatformUnkonwn;
}

- (NSString *)platformName
{
    switch ([self platform]) {
        case PlatformiPhone:
            return @"iPhone";
        case PlatformiPhone3G:
            return @"iPhone3G";
        case PlatformiPhone3GS:
            return @"iPhone3GS";
        case PlatformiPhone4_GSM:
        case PlatformiPhone4_RevA:
        case PlatformiPhone4_CDMA:
            return @"iPhone4";
        case PlatformiPhone4S:
            return @"iPhone4S";
        case PlatformiPhone5_GSM:
        case PlatformiPhone5_CDMA:
            return @"iPhone5";
        case PlatformiPhone5c_GSM:
        case PlatformiPhone5c_CDMA:
            return @"iPhone5c";
        case PlatformiPhone5s_GSM:
        case PlatformiPhone5s_CDMA:
            return @"iPhone5s";
        case PlatformiPhone6Plus:
            return @"iPhone6 Plus";
        case PlatformiPhone6:
            return @"iPhone6";
        case PlatformiPodTouch1G:
            return @"iPod touch 1st Gen";
        case PlatformiPodTouch2G:
            return @"iPod touch 2nd Gen";
        case PlatformiPodTouch3G:
            return @"iPod touch 3rd Gen";
        case PlatformiPodTouch4G:
            return @"iPod touch 4th Gen";
        case PlatformiPodTouch5G:
            return @"iPod touch 5th Gen";
        case PlatformiPad_Wifi:
            return @"iPad";
        case PlatformiPad2_WiFi:
        case PlatformiPad2_GSM:
        case PlatformiPad2_CDMA:
        case PlatformiPad2_RevA:
            return @"iPad2";
        case PlatformiPadMini_WiFi:
        case PlatformiPadMini_GSM:
        case PlatformiPadMini_CDMA:
            return @"iPad mini";
        case PlatformiPad3_WiFi:
        case PlatformiPad3_GSM:
        case PlatformiPad3_CDMA:
            return @"iPad3";
        case PlatformiPad4_WiFi:
        case PlatformiPad4_GSM:
        case PlatformiPad4_CDMA:
            return @"iPad4";
        case PlatformiPadAir_WiFi:
        case PlatformiPadAir_Cellular:
        case PlatformiPadAir_China:
            return @"iPad Air";
        case PlatformiPadMini2_WiFi:
        case PlatformiPadMini2_Cellular:
        case PlatformiPadMini2_China:
            return @"iPad mini 2";
        case PlatformiPadMini3_WiFi:
        case PlatformiPadMini3_Cellular:
        case PlatformiPadMini3_China:
            return @"iPad mini 3";
        case PlatformiPadAir2_WiFi:
        case PlatformiPadAir2_Cellular:
            return @"iPad Air 2";
        case PlatformAppleTV1G:
        case PlatformAppleTV2G:
            return @"Apple TV";
        case Platformi386Simulator:
        case Platformx86_64Simulator:
            return @"Simulator";
        case PlatformUnkonwn:
        default:
            return @"Unknown";
    }
}