34

iPhoneの機種によって機能が異なるアプリを書いています。iPhone 5 と 5c 5s を見分ける方法はありますか? 同一品のため表示の確認はできません。前もって感謝します。

4

6 に答える 6

89

Objective-C と SWIFT

これは、iOS 6、iOS 7、iOS 8 などのすべてのバージョンで動作しています...

iPhone 6 & iPhone 6 Plus用にアップデート

 -(void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.  

    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];
    NSLog(@"iPhone Device%@",[self platformType:platform]);
    
    free(machine);
}


- (NSString *) platformType:(NSString *)platform
{
    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:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
    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:@"iPod5,1"])      return @"iPod Touch 5G";
    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:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini 2G (Cellular)";
    if ([platform isEqualToString:@"iPad4,6"])      return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (WiFi)";
    if ([platform isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
    if ([platform isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
    if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3";
    if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3 (2013)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}

このコードは AppStore でも安全です。

スイフト 3.2

    override func viewDidLoad() {
    super.viewDidLoad()

    var sysInfo = utsname()
    uname(&sysInfo)
    let machine = Mirror(reflecting: sysInfo.machine)
    let identifier = machine.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }
    
    NSLog("Device Type ----> %@", self.platformType(platform: identifier as NSString));
    
    print(self.platformType(platform: identifier as NSString))
}




 func platformType(platform : NSString) -> NSString{
    if platform.isEqual(to: "iPhone1,1")  {
        return "iPhone 1G"
    }
    else if platform.isEqual(to: "iPhone1,2"){
        return "iPhone 3G"
    }
    else if platform.isEqual(to: "iPhone2,1"){
        return "iPhone 3GS"
    }
    else if platform.isEqual(to: "iPhone3,1"){
        return "iPhone 4"
    }
    else if platform.isEqual(to: "iPhone3,3"){
        return "Verizon iPhone 4"
    }
    else if platform.isEqual(to: "iPhone4,1"){
        return "iPhone 4S"
    }
    else if platform.isEqual(to: "iPhone5,1"){
        return "iPhone 5 (GSM)"
    }
    else if platform.isEqual(to: "iPhone5,2"){
        return "iPhone 5 (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPhone5,3"){
        return "iPhone 5c (GSM)"
    }
    else if platform.isEqual(to: "iPhone5,4"){
        return "iPhone 5c (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPhone6,1"){
        return "iPhone 5s (GSM)"
    }
    else if platform.isEqual(to: "iPhone6,2"){
        return "iPhone 5s (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPhone7,2"){
        return "iPhone 6"
    }
    else if platform.isEqual(to: "iPhone7,1"){
        return "iPhone 6 Plus"
    }
    else if platform.isEqual(to: "iPhone8,1"){
        return "iPhone 6s"
    }
    else if platform.isEqual(to: "iPhone8,2"){
        return "iPhone 6s Plus"
    }
    else if platform.isEqual(to: "iPhone8,4"){
        return "iPhone SE"
    }
    else if platform.isEqual(to: "iPhone9,1"){
        return "iPhone 7"
    }
    else if platform.isEqual(to: "iPhone9,2"){
        return "iPhone 7 Plus"
    }
    else if platform.isEqual(to: "iPhone9,3"){
        return "iPhone 7"
    }
    else if platform.isEqual(to: "iPhone9,4"){
        return "iPhone 7 Plus"
    }
    else if platform.isEqual(to: "iPod1,1"){
        return "iPod Touch 1G"
    }
    else if platform.isEqual(to: "iPod2,1"){
        return "iPod Touch 2G"
    }
    else if platform.isEqual(to: "iPod3,1"){
        return "iPod Touch 3G"
    }
    else if platform.isEqual(to: "iPod4,1"){
        return "iPod Touch 4G"
    }
    else if platform.isEqual(to: "iPod5,1"){
        return "iPod Touch 5G"
    }
    else if platform.isEqual(to: "iPad1,1"){
        return "iPad"
    }
    else if platform.isEqual(to: "iPad2,1"){
        return "iPad 2 (WiFi)"
    }
    else if platform.isEqual(to: "iPad2,2"){
        return "iPad 2 (GSM)"
    }
    else if platform.isEqual(to: "iPad2,3"){
        return "iPad 2 (CDMA)"
    }
    else if platform.isEqual(to: "iPad2,4"){
        return "iPad 2 (WiFi)"
    }
    else if platform.isEqual(to: "iPad2,5"){
        return "iPad Mini (WiFi)"
    }
    else if platform.isEqual(to: "iPad2,6"){
        return "iPad Mini (GSM)"
    }
    else if platform.isEqual(to: "iPad2,7"){
        return "iPad Mini (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPad3,1"){
        return "iPad 3 (WiFi)"
    }
    else if platform.isEqual(to: "iPad3,2"){
        return "iPad 3 (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPad3,3"){
        return "iPad 3 (GSM)"
    }
    else if platform.isEqual(to: "iPad3,4"){
        return "iPad 4 (WiFi)"
    }
    else if platform.isEqual(to: "iPad3,5"){
        return "iPad 4 (GSM)"
    }
    else if platform.isEqual(to: "iPad3,6"){
        return "iPad 4 (GSM+CDMA)"
    }
    else if platform.isEqual(to: "iPad4,1"){
        return "iPad Air (WiFi)"
    }
    else if platform.isEqual(to: "iPad4,2"){
        return "iPad Air (Cellular)"
    }
    else if platform.isEqual(to: "iPad4,3"){
        return "iPad Air"
    }
    else if platform.isEqual(to: "iPad4,4"){
        return "iPad Mini 2G (WiFi)"
    }
    else if platform.isEqual(to: "iPad4,5"){
        return "iPad Mini 2G (Cellular)"
    }
    else if platform.isEqual(to: "iPad4,6"){
        return "iPad Mini 2G";
    }
    else if platform.isEqual(to: "iPad4,7"){
        return "iPad Mini 3 (WiFi)"
    }
    else if platform.isEqual(to: "iPad4,8"){
        return "iPad Mini 3 (Cellular)"
    }
    else if platform.isEqual(to: "iPad4,9"){
        return "iPad Mini 3 (China)"
    }
    else if platform.isEqual(to: "iPad5,3"){
        return "iPad Air 2 (WiFi)"
    }
    else if platform.isEqual(to: "iPad5,4"){
        return "iPad Air 2 (Cellular)"
    }
    else if platform.isEqual(to: "AppleTV2,1"){
        return "Apple TV 2G"
    }
    else if platform.isEqual(to: "AppleTV3,1"){
        return "Apple TV 3"
    }
    else if platform.isEqual(to: "AppleTV3,2"){
        return "Apple TV 3 (2013)"
    }
    else if platform.isEqual(to: "i386"){
        return "Simulator"
    }
    else if platform.isEqual(to: "x86_64"){
        return "Simulator"
    }
    return platform
}
于 2013-10-25T08:12:35.130 に答える
11

これを使用して、iPhone6,2 や iPhone 3,1 などのデバイス モデル識別子を取得します。次に、ifステートメントを実行して、識別子がプレフィックスと等しいかどうかを確認し、NSStringに正しい名前を割り当てることができます。

コードは次のとおりです。

#import <sys/utsname.h>

NSString *deviceType;
struct utsname systemInfo;
uname(&systemInfo);
deviceType = [NSString stringWithCString:systemInfo.machine
                                encoding:NSUTF8StringEncoding];
于 2013-10-25T08:15:53.633 に答える
5

ここに解決策があります..

1) アップデート iPhone 6s (8,1) および 6s plus (8,2) を含むデバイス リストを定義します ::

//DeviceList
#define HARDWARE @{
@"i386": @"Simulator",
@"x86_64": @"Simulator",
@"iPod1,1": @"iPod Touch",
@"iPod2,1": @"iPod Touch 2nd Generation",
@"iPod3,1": @"iPod Touch 3rd Generation",
@"iPod4,1": @"iPod Touch 4th Generation",
@"iPhone1,1": @"iPhone",
@"iPhone1,2": @"iPhone 3G",
@"iPhone2,1": @"iPhone 3GS",
@"iPhone3,1": @"iPhone 4",
@"iPhone4,1": @"iPhone 4S",
@"iPhone5,1": @"iPhone 5",
@"iPhone5,2": @"iPhone 5",
@"iPhone5,3": @"iPhone 5c",
@"iPhone5,4": @"iPhone 5c",
@"iPhone6,1": @"iPhone 5s",
@"iPhone6,2": @"iPhone 5s",
@"iPad1,1": @"iPad",
@"iPad2,1": @"iPad 2",
@"iPad3,1": @"iPad 3rd Generation ",
@"iPad3,4": @"iPad 4th Generation ",
@"iPad2,5": @"iPad Mini",
@"iPad4,4": @"iPad Mini 2nd Generation - Wifi",
@"iPad4,5": @"iPad Mini 2nd Generation - Cellular",
@"iPad4,1": @"iPad Air 5th Generation - Wifi",
@"iPad4,2": @"iPad Air 5th Generation - Cellular",
@"iPhone7,1": @"iPhone 6 Plus",
@"iPhone7,2": @"iPhone 6",
@"iPhone8,1": @"iPhone 6S (GSM+CDMA)",
@"iPhone8,2": @"iPhone 6S+ (GSM+CDMA)"
}

2)ヘッダーファイルを追加::

#import <sys/utsname.h>

3) デバイス名を取得するには::

    //To get the Device_name
    struct utsname systemInfo;
    uname(&systemInfo);
    deviceName=[HARDWARE objectForKey:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]];
    NSLog(@"Device Name::%@",deviceName);
于 2014-11-24T04:38:08.737 に答える
1

このライブラリを試してください:

https://github.com/erica/uidevice-extension/

次のようなものを取得できます。

[[UIDevice currentDevice] platformType];   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString]; // ex: @"iPhone 4G"
于 2013-10-25T07:58:32.433 に答える
0

私は自分が少し先を行っていることを知っていますが、iPhone 6 のプラットフォームを知っている人はいますか? したがって、上記のリストに追加された場合、それはiPhone6,3(iPhone6,4単なるワイルド ゲスト...)

if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
于 2014-08-15T05:28:04.783 に答える
-5

「UIDevice」クラスのモデル プロパティを確認してください。または、このコードを使用してみてください

NSString *deviceType = [UIDevice currentDevice].model;
NSLog(@"DEVICE TYPE %@", deviceType);

このリンクをチェックしてください

お役に立てれば

于 2013-10-25T08:01:24.547 に答える