Déterminer l'appareil (iPhone, iPod Touch) avec iOS

371

Existe-t-il un moyen de déterminer le périphérique exécutant une application. Je veux faire la distinction entre iPhoneet iPod Touch, si possible.

Rob
la source
1
La classe UIDeviceHardware utilise-t-elle des bibliothèques non documentées et donc interdites?
4
Non, ils sont documentés. developer.apple.com/iphone/library/documentation/System/…
lawrence
Il y a de très bonnes solutions dans le fil qui contient cette réponse
clearlight

Réponses:

286

Vous pouvez utiliser la UIDeviceclasse comme ceci:

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])
    // it's an iPhone
Adam Rosenfield
la source
4
@jeeva: Le code de ce lien est une vérification à la compilation pour les applications universelles (applications qui sont compilées séparément pour iPhone et iPad). Ce code est une vérification d'exécution. Gardez également à l'esprit que cette question et cette réponse ont été écrites bien avant que l'iPad n'existe.
Adam Rosenfield,
@Adam, je suis d'accord avec vous .. vous avez écrit très longtemps une réponse qui est du bon travail .... j'ai dit qu'au lieu d'utiliser le modèle, vous pouvez utiliser UIUserInterfaceIdiomPad qui est donné à cet effet uniquement ...
@jeeva, je ne pense pas que cela soit pris en charge dans le système d'exploitation 3.0
Anil Sivadas
1
puis-je suggérer de ne pas marquer cela comme la meilleure réponse et de marquer celle qui suit, avec le code de classe UIDeviceHardware complet, comme le meilleur, s'il vous plaît?
snibbe
1
@AnilSivadas: UI_USER_INTERFACE_IDIOM() est sûr à utiliser dans les applications qui prennent toujours en charge iOS <3.2: il s'agit d'une macro UIDevice.hspécifiquement écrite par défaut UIUserInterfaceIdiomPhonesur ces anciennes versions d'iOS.
mklement0
278

Il s'agit d'une mise à jour UIDeviceHardware.mde la réponse ci-dessus .

- (NSString *)platformString
{
    NSString *platform = [self 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,1"])    return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,2"])    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:@"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:@"iPod7,1"])      return @"iPod Touch 6G";
    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,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad mini 4 (WiFi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad mini 4 (Cellular)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7 inch (Cellular)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9 inch (WiFi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9 inch (Cellular)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}
Brian Robbins
la source
5
Désolé, cette fonction n'entraînera-t-elle pas une boucle infinie en raison de la première ligne? Qui est «moi» ici?
jakev
Oui, j'ai annulé ce changement
Matthias Bauch
4
Pourquoi le nom iPad-3G? Est-ce le nom officiel du nouvel iPad? Je pense que l'iPad 4G ou l'iPad 3 est plus logique.
Stunner
4
Le nom d'une NSStringcomparaison en cascade donnant NSStringest NSDictionary.Le nom pour en définir une sans dépendances de code est .plist.
Fritz Anderson
6
Pour info - il semble qu'une liste mise à jour soit conservée ici - theiphonewiki.com/wiki/Models
user1021430
122

N'hésitez pas à utiliser cette classe ( gist @ github )

CODE SUPPRIMÉ ET RELOCALISÉ À

https://gist.github.com/1323251

MISE À JOUR (14/01/11)

Évidemment, ce code est un peu obsolète à l'heure actuelle, mais il peut certainement être mis à jour en utilisant le code sur ce fil fourni par Brian Robbinsqui comprend un code similaire avec des modèles mis à jour. Merci pour le soutien sur ce fil.

Dutchie432
la source
3
@Dave: Le code ne casse pas, il renvoie simplement une chaîne légèrement moins jolie. Avez-vous une meilleure méthode pour obtenir le modèle d'appareil? Et par modèle, je veux dire plus que «iPhone» ou «iPod», mais la version spécifique.
Winsmith
7
Je ne comprends pas pourquoi vous ne les définissez pas comme des méthodes de classe, car vous ne maintenez aucun état. Remplacez simplement - (NSString *) par + (NSString *) et vous vous épargnerez une allocation / version lors de son utilisation.
Ben G
1
Cette ligne: NSString * platform = [NSString stringWithCString: machine]; , doit être NSString * platform = [NSString stringWithUTF8String: machine]; puisque stringWithCString est obsolète.
Goles
1
@Oliver - Ajouté. C'est "iPhone4,1"
DougW
1
Ma suggestion est d'ajouter ce morceau de code assez simple en tant qu'essentiel dans github, ou en tant que très petit projet open source. Étant donné que Stack Overflow n'est pas adapté à ce type de développement;)
Goles
37

J'utilise ceci dans mon application. À jour en décembre 2012.

- (NSString *) platformString {
    // Gets a string with the device model
    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 @"iPhone 2G";
    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,2"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (CDMA)";    
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";

    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch (1 Gen)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch (2 Gen)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch (3 Gen)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4 Gen)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";

    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    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";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";

    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}  
Tomasz
la source
8
il lançant un avertissement disant "La déclaration implicite de la fonction 'sysctlbyname' n'est pas valide en C99" à la ligne sysctlbyname ("hw.machine", NULL, & size, NULL, 0);
Ans
Pour résoudre l'avertissement "Déclaration implicite de fonction ..", vous devez ajouter ce qui suit: #import <sys/sysctl.h>
Brabbeldas
29

Dernière mise à jour - octobre 2019: iPhone 11 , iPhone 11 Pro , iPhone 11 Pro Max et iPad ajoutés (7e génération) .

Mise à jour précédente - mars 2019: ajout de l' iPad Air 2019 (3e génération), de l' iPad mini 2019 (5e génération), de l' iPod Touch 2019 (7e génération)


Rapide

(Testé sur Swift 5, Swift 4, Swift 3)

La fonction ci-dessous renvoie une chaîne avec le nom du périphérique actuel.

func userDeviceName() -> String {
    let platform: String = {
        var size = 0
        sysctlbyname("hw.machine", nil, &size, nil, 0)
        var machine = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname("hw.machine", &machine, &size, nil, 0)
        return String(cString: machine)
    }()

    //iPhone
    if platform == "iPhone1,1"        { return "iPhone (1st generation)" }
    else if platform == "iPhone1,2"   { return "iPhone 3G" }
    else if platform == "iPhone2,1"   { return "iPhone 3GS" }
    else if platform == "iPhone3,1"   { return "iPhone 4 (GSM)" }
    else if platform == "iPhone3,2"   { return "iPhone 4 (GSM, 2nd revision)" }
    else if platform == "iPhone3,3"   { return "iPhone 4 (CDMA)" }
    else if platform == "iPhone4,1"   { return "iPhone 4S" }
    else if platform == "iPhone5,1"   { return "iPhone 5 (GSM)" }
    else if platform == "iPhone5,2"   { return "iPhone 5 (GSM+CDMA)" }
    else if platform == "iPhone5,3"   { return "iPhone 5c (GSM)" }
    else if platform == "iPhone5,4"   { return "iPhone 5c (GSM+CDMA)" }
    else if platform == "iPhone6,1"   { return "iPhone 5s (GSM)" }
    else if platform == "iPhone6,2"   { return "iPhone 5s (GSM+CDMA)" }
    else if platform == "iPhone7,2"   { return "iPhone 6" }
    else if platform == "iPhone7,1"   { return "iPhone 6 Plus" }
    else if platform == "iPhone8,1"   { return "iPhone 6s" }
    else if platform == "iPhone8,2"   { return "iPhone 6s Plus" }
    else if platform == "iPhone8,4"   { return "iPhone SE" }
    else if platform == "iPhone9,1"   { return "iPhone 7 (GSM+CDMA)" }
    else if platform == "iPhone9,3"   { return "iPhone 7 (GSM)" }
    else if platform == "iPhone9,2"   { return "iPhone 7 Plus (GSM+CDMA)" }
    else if platform == "iPhone9,4"   { return "iPhone 7 Plus (GSM)" }
    else if platform == "iPhone10,1"  { return "iPhone 8 (GSM+CDMA)" }
    else if platform == "iPhone10,4"  { return "iPhone 8 (GSM)" }
    else if platform == "iPhone10,2"  { return "iPhone 8 Plus (GSM+CDMA)" }
    else if platform == "iPhone10,5"  { return "iPhone 8 Plus (GSM)" }
    else if platform == "iPhone10,3"  { return "iPhone X (GSM+CDMA)" }
    else if platform == "iPhone10,6"  { return "iPhone X (GSM)" }
    else if platform == "iPhone11,2"  { return "iPhone XS" }
    else if platform == "iPhone11,6"  { return "iPhone XS Max" }
    else if platform == "iPhone11,8"  { return "iPhone XR" }
    else if platform == "iPhone12,1"  { return "iPhone 11" }
    else if platform == "iPhone12,3"  { return "iPhone 11 Pro" }
    else if platform == "iPhone12,5"  { return "iPhone 11 Pro Max" }

    //iPod Touch
    else if platform == "iPod1,1"     { return "iPod Touch (1st generation)" }
    else if platform == "iPod2,1"     { return "iPod Touch (2nd generation)" }
    else if platform == "iPod3,1"     { return "iPod Touch (3rd generation)" }
    else if platform == "iPod4,1"     { return "iPod Touch (4th generation)" }
    else if platform == "iPod5,1"     { return "iPod Touch (5th generation)" }
    else if platform == "iPod7,1"     { return "iPod Touch (6th generation)" }
    else if platform == "iPod9,1"     { return "iPod Touch (7th generation)" }

    //iPad
    else if platform == "iPad1,1"     { return "iPad (1st generation)" }
    else if platform == "iPad2,1"     { return "iPad 2 (Wi-Fi)" }
    else if platform == "iPad2,2"     { return "iPad 2 (GSM)" }
    else if platform == "iPad2,3"     { return "iPad 2 (CDMA)" }
    else if platform == "iPad2,4"     { return "iPad 2 (Wi-Fi, Mid 2012)" }
    else if platform == "iPad3,1"     { return "iPad (3rd generation) (Wi-Fi)" }
    else if platform == "iPad3,2"     { return "iPad (3rd generation) (GSM+CDMA)" }
    else if platform == "iPad3,3"     { return "iPad (3rd generation) (GSM)" }
    else if platform == "iPad3,4"     { return "iPad (4th generation) (Wi-Fi)"}
    else if platform == "iPad3,5"     { return "iPad (4th generation) (GSM)" }
    else if platform == "iPad3,6"     { return "iPad (4th generation) (GSM+CDMA)" }
    else if platform == "iPad6,11"    { return "iPad (5th generation) (Wi-Fi)" }
    else if platform == "iPad6,12"    { return "iPad (5th generation) (Cellular)" }
    else if platform == "iPad7,5"     { return "iPad (6th generation) (Wi-Fi)" }
    else if platform == "iPad7,6"     { return "iPad (6th generation) (Cellular)" }
    else if platform == "iPad7,11"     { return "iPad (7th generation) (Wi-Fi)" }
    else if platform == "iPad7,12"     { return "iPad (7th generation) (Cellular)" }

    //iPad Mini
    else if platform == "iPad2,5"     { return "iPad mini (Wi-Fi)" }
    else if platform == "iPad2,6"     { return "iPad mini (GSM)" }
    else if platform == "iPad2,7"     { return "iPad mini (GSM+CDMA)" }
    else if platform == "iPad4,4"     { return "iPad mini 2 (Wi-Fi)" }
    else if platform == "iPad4,5"     { return "iPad mini 2 (Cellular)" }
    else if platform == "iPad4,6"     { return "iPad mini 2 (China)" }
    else if platform == "iPad4,7"     { return "iPad mini 3 (Wi-Fi)" }
    else if platform == "iPad4,8"     { return "iPad mini 3 (Cellular)" }
    else if platform == "iPad4,9"     { return "iPad mini 3 (China)" }
    else if platform == "iPad5,1"     { return "iPad mini 4 (Wi-Fi)" }
    else if platform == "iPad5,2"     { return "iPad mini 4 (Cellular)" }
    else if platform == "iPad11,1"    { return "iPad mini (5th generation) (Wi-Fi)" }
    else if platform == "iPad11,2"    { return "iPad mini (5th generation)  (Cellular)" }

    //iPad Air
    else if platform == "iPad4,1"     { return "iPad Air (Wi-Fi)" }
    else if platform == "iPad4,2"     { return "iPad Air (Cellular)" }
    else if platform == "iPad4,3"     { return "iPad Air (China)" }
    else if platform == "iPad5,3"     { return "iPad Air 2 (Wi-Fi)" }
    else if platform == "iPad5,4"     { return "iPad Air 2 (Cellular)" }
    else if platform == "iPad11,3"    { return "iPad Air (3rd generation) (Wi-Fi)" }
    else if platform == "iPad11,4"    { return "iPad Air (3rd generation) (Cellular)" }

    //iPad Pro
    else if platform == "iPad6,3"     { return "iPad Pro 9.7\" (Wi-Fi)" }
    else if platform == "iPad6,4"     { return "iPad Pro 9.7\" (Cellular)" }
    else if platform == "iPad6,7"     { return "iPad Pro 12.9\" (Wi-Fi)" }
    else if platform == "iPad6,8"     { return "iPad Pro 12.9\" (Cellular)" }
    else if platform == "iPad7,1"     { return "iPad Pro 12.9\" (2nd generation) (Wi-Fi)" }
    else if platform == "iPad7,2"     { return "iPad Pro 12.9\" (2nd generation) (Cellular)" }
    else if platform == "iPad7,3"     { return "iPad Pro 10.5\" (Wi-Fi)" }
    else if platform == "iPad7,4"     { return "iPad Pro 10.5\" (Cellular)" }
    else if platform == "iPad8,1"     { return "iPad Pro 11\" (Wi-Fi)" }
    else if platform == "iPad8,2"     { return "iPad Pro 11\" (Wi-Fi, 1TB)" }
    else if platform == "iPad8,3"     { return "iPad Pro 11\" (Cellular)" }
    else if platform == "iPad8,4"     { return "iPad Pro 11\" (Cellular 1TB)" }
    else if platform == "iPad8,5"     { return "iPad Pro 12.9\" (3rd generation) (Wi-Fi)" }
    else if platform == "iPad8,6"     { return "iPad Pro 12.9\" (3rd generation) (Cellular)" }
    else if platform == "iPad8,7"     { return "iPad Pro 12.9\" (3rd generation) (Wi-Fi, 1TB)" }
    else if platform == "iPad8,8"     { return "iPad Pro 12.9\" (3rd generation) (Cellular, 1TB)" }

    //Apple TV
    else if platform == "AppleTV2,1"  { return "Apple TV 2G" }
    else if platform == "AppleTV3,1"  { return "Apple TV 3" }
    else if platform == "AppleTV3,2"  { return "Apple TV 3 (2013)" }
    else if platform == "AppleTV5,3"  { return "Apple TV 4" }
    else if platform == "AppleTV6,2"  { return "Apple TV 4K" }

    //Apple Watch
    else if platform == "Watch1,1"    { return "Apple Watch (1st generation) (38mm)" }
    else if platform == "Watch1,2"    { return "Apple Watch (1st generation) (42mm)" }
    else if platform == "Watch2,6"    { return "Apple Watch Series 1 (38mm)" }
    else if platform == "Watch2,7"    { return "Apple Watch Series 1 (42mm)" }
    else if platform == "Watch2,3"    { return "Apple Watch Series 2 (38mm)" }
    else if platform == "Watch2,4"    { return "Apple Watch Series 2 (42mm)" }
    else if platform == "Watch3,1"    { return "Apple Watch Series 3 (38mm Cellular)" }
    else if platform == "Watch3,2"    { return "Apple Watch Series 3 (42mm Cellular)" }
    else if platform == "Watch3,3"    { return "Apple Watch Series 3 (38mm)" }
    else if platform == "Watch3,4"    { return "Apple Watch Series 3 (42mm)" }
    else if platform == "Watch4,1"    { return "Apple Watch Series 4 (40mm)" }
    else if platform == "Watch4,2"    { return "Apple Watch Series 4 (44mm)" }
    else if platform == "Watch4,3"    { return "Apple Watch Series 4 (40mm Cellular)" }
    else if platform == "Watch4,4"    { return "Apple Watch Series 4 (44mm Cellular)" }
    //else if platform == "Watch"    { return "Apple Watch Series 5 (40mm)" } //5,1?
    //else if platform == "Watch"    { return "Apple Watch Series 5 (44mm)" } //5,2?
    //else if platform == "Watch"    { return "Apple Watch Series 5 (40mm Cellular)" } //5,3?
    //else if platform == "Watch"    { return "Apple Watch Series 5 (44mm Cellular)" } //5,4?

    //Simulator
    else if platform == "i386"        { return "Simulator" }
    else if platform == "x86_64"      { return "Simulator"}

    return platform
}

Vous pouvez le tester en utilisant le code suivant:

print("Current device is: ", self.userDeviceName())

Objectif c

J'ai également ajouté de nouveaux appareils au code Objective-C

N'oubliez pas d'importer sys / sysctl.h

 #import <sys/sysctl.h>  


- (NSString *) userDeviceName {
    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);

    //iPhone
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone (1st generation)";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4 (GSM)";
    if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 (GSM, 2nd revision)";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"iPhone 4 (Verizon)";
    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 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone9,3"])    return @"iPhone 7 (GSM)";
    if ([platform isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus (GSM)";
    if ([platform isEqualToString:@"iPhone10,1"])   return @"iPhone 8 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,4"])   return @"iPhone 8 (GSM)";
    if ([platform isEqualToString:@"iPhone10,2"])   return @"iPhone 8 Plus (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,5"])   return @"iPhone 8 Plus (GSM)";
    if ([platform isEqualToString:@"iPhone10,3"])   return @"iPhone X (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone10,6"])   return @"iPhone X (GSM)";
    if ([platform isEqualToString:@"iPhone11,2"])   return @"iPhone XS";
    if ([platform isEqualToString:@"iPhone11,6"])   return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone11,8"])   return @"iPhone XR";
    if ([platform isEqualToString:@"iPhone12,1"])   return @"iPhone 11";
    if ([platform isEqualToString:@"iPhone12,3"])   return @"iPhone 11 Pro";
    if ([platform isEqualToString:@"iPhone12,5"])   return @"iPhone 11 Pro Max";

    //iPod Touch
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch (1st generation)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch (2nd generation)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch (3rd generation)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4th generation)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5th generation)";
    if ([platform isEqualToString:@"iPod7,1"])      return @"iPod Touch (6th generation)";
    if ([platform isEqualToString:@"iPod9,1"])      return @"iPod Touch (7th generation)";

    //iPad
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad (1st generation)";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (Wi-Fi)";
    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 (Wi-Fi, Mid 2012)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad (3rd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad (3rd generation) (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad (3rd generation) (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad (4th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad (4th generation) (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad (4th generation) (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad6,11"])     return @"iPad (5th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,12"])     return @"iPad (5th generation) (Cellular)";
    if ([platform isEqualToString:@"iPad7,5"])      return @"iPad (6th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,6"])      return @"iPad (6th generation) (Cellular)";
    if ([platform isEqualToString:@"iPad7,11"])      return @"iPad (7th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,12"])      return @"iPad (7th generation) (Cellular)";

    //iPad Air
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air (China)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad11,3"])     return @"iPad Air (3rd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad11,4"])     return @"iPad Air (3rd generation) (Cellular)";

    //iPad Pro
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7\" (Cellular)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9\" (Cellular)";
    if ([platform isEqualToString:@"iPad7,1"])      return @"iPad Pro 12.9\" (2nd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,2"])      return @"iPad Pro 12.9\" (2nd generation) (Cellular)";
    if ([platform isEqualToString:@"iPad7,3"])      return @"iPad Pro 10.5\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad7,4"])      return @"iPad Pro 10.5\" (Cellular)";
    if ([platform isEqualToString:@"iPad8,1"])      return @"iPad Pro 11\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad8,2"])      return @"iPad Pro 11\" (Wi-Fi, 1TB)";
    if ([platform isEqualToString:@"iPad8,3"])      return @"iPad Pro 11\" (Wi-Fi)";
    if ([platform isEqualToString:@"iPad8,4"])      return @"iPad Pro 11\" (Cellular)";
    if ([platform isEqualToString:@"iPad8,5"])      return @"iPad Pro 12.9\" (3rd generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad8,6"])      return @"iPad Pro 12.9\" (3rd generation) (Cellular)";
    if ([platform isEqualToString:@"iPad8,7"])      return @"iPad Pro 12.9\" (3rd generation) (Wi-Fi, 1TB)";
    if ([platform isEqualToString:@"iPad8,8"])      return @"iPad Pro 12.9\" (3rd generation) (Cellular, 1TB)";

    //iPad mini
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad mini (Wi-Fi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2 (Cellular)";
    if ([platform isEqualToString:@"iPad4,6"])      return @"iPad mini 2 (China)";
    if ([platform isEqualToString:@"iPad4,7"])      return @"iPad mini 3 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad4,8"])      return @"iPad mini 3 (Cellular)";
    if ([platform isEqualToString:@"iPad4,9"])      return @"iPad mini 3 (China)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad mini 4 (Wi-Fi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad mini 4 (Cellular)";
    if ([platform isEqualToString:@"iPad11,1"])     return @"iPad mini (5th generation) (Wi-Fi)";
    if ([platform isEqualToString:@"iPad11,2"])     return @"iPad mini (5th generation) (Cellular)";

    //Apple TV
    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:@"AppleTV5,3"])   return @"Apple TV 4";
    if ([platform isEqualToString:@"AppleTV6,2"])   return @"Apple TV 4K";

    //Apple Watch
    if ([platform isEqualToString:@"Watch1,1"])     return @"Apple Watch (1st generation) (38mm)";
    if ([platform isEqualToString:@"Watch1,2"])     return @"Apple Watch (1st generation) (42mm)";
    if ([platform isEqualToString:@"Watch2,6"])     return @"Apple Watch Series 1 (38mm)";
    if ([platform isEqualToString:@"Watch2,7"])     return @"Apple Watch Series 1 (42mm)";
    if ([platform isEqualToString:@"Watch2,3"])     return @"Apple Watch Series 2 (38mm)";
    if ([platform isEqualToString:@"Watch2,4"])     return @"Apple Watch Series 2 (42mm)";
    if ([platform isEqualToString:@"Watch3,1"])     return @"Apple Watch Series 3 (38mm Cellular)";
    if ([platform isEqualToString:@"Watch3,2"])     return @"Apple Watch Series 3 (42mm Cellular)";
    if ([platform isEqualToString:@"Watch3,3"])     return @"Apple Watch Series 3 (38mm)";
    if ([platform isEqualToString:@"Watch3,4"])     return @"Apple Watch Series 3 (42mm)";
    if ([platform isEqualToString:@"Watch4,1"])     return @"Apple Watch Series 4 (40mm)";
    if ([platform isEqualToString:@"Watch4,2"])     return @"Apple Watch Series 4 (44mm)";
    if ([platform isEqualToString:@"Watch4,3"])     return @"Apple Watch Series 4 (40mm Cellular)";
    if ([platform isEqualToString:@"Watch4,4"])     return @"Apple Watch Series 4 (44mm Cellular)";
    //if ([platform isEqualToString:@"Watch"])     return @"Apple Watch Series 5 (40mm)";
    //if ([platform isEqualToString:@"Watch"])     return @"Apple Watch Series 5 (44mm)";
    //if ([platform isEqualToString:@"Watch"])     return @"Apple Watch Series 5 (40mm Cellular)";
    //if ([platform isEqualToString:@"Watch"])     return @"Apple Watch Series 5 (44mm Cellular)";

    //Simulator
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}

Vous pouvez le tester en utilisant le code suivant:

NSLog(@"Current device is: %@", [self userDeviceName]);
D1mers0n
la source
Juste pour clarifier ... iPhone7,2est le 6 mais iPhone8,1est le 6S? ils ont inversé pour les 6 et 6S?
Magoo
2
Oui, c'est correct. Vous pouvez vérifier ici: theiphonewiki.com/wiki/Models
D1mers0n
Consultez ce github.com/aleemrazzaq/ARCompactDeviceInfo qui a le dernier modèle d'appareil, iPhone X, iPhone 8, 8 Plus et iPad 6e génération 2018
Aleem
Une façon de vérifier le type de simulateur? :)
Victor Engel
Cet article semble avoir la réponse à ma requête: stackoverflow.com/questions/33493800/…
Victor Engel
27
if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) {
    //Device is ipad 
}else{
    //Device is iphone
}
Gurpreet Singh
la source
6
Si vous regardez la question que Rob mentionne "Je veux faire la distinction entre iPhone et iPod Touch si possible". Votre solution ne résout pas ce problème.
stephenmuss
14

Chaînes de plateforme mises à jour pour iPad Air 2et iPad mini 3:

- (NSString *)platformString
{
    NSString *platform = [self 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,1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    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,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";

    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 Model)";

    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro";

    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

    return platform;
}
uplearnedu.com
la source
12

Plus utilisable

#include <sys/types.h>
#include <sys/sysctl.h>

@interface UIDevice(Hardware)

- (NSString *) platform;

- (BOOL)hasRetinaDisplay;

- (BOOL)hasMultitasking;

- (BOOL)hasCamera;

@end

@implementation UIDevice(Hardware)

- (NSString *) platform{
    int mib[2];
size_t len;
char *machine;

mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);

    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
return platform;
}

- (BOOL)hasRetinaDisplay {
    NSString *platform = [self platform];
    BOOL ret = YES;
    if ([platform isEqualToString:@"iPhone1,1"]) {
        ret = NO;
    }
    else
        if ([platform isEqualToString:@"iPhone1,2"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPhone2,1"])    ret = NO;
    else 
        if ([platform isEqualToString:@"iPod1,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod2,1"])      ret = NO;
    else
        if ([platform isEqualToString:@"iPod3,1"])      ret = NO;
    return ret;
}

- (BOOL)hasMultitasking {
    if ([self respondsToSelector:@selector(isMultitaskingSupported)]) {
        return [self isMultitaskingSupported];
    }
    return NO;
}

- (BOOL)hasCamera {
   BOOL ret = NO;
   // check camera availability
   return ret;
}

@end

vous pouvez lire les propriétés avec

NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO");
UIBuilder
la source
4
Au lieu d'un arbre if / else compliqué, vous pouvez simplement vérifier si [[UIScreen mainScreen] scale]c'est 2 ou non (pour iOS 5+).
Todd Lehman
N'inclut pas la fonction "hasVibration" nécessaire.
Jonny
10

Voici une mise à jour mineure avec de nouveaux modèles:

- (NSString *) platformString{
    NSString *platform = [self 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:@"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:@"i386"])   return @"iPhone Simulator";
    return platform;
}
snibbe
la source
10

Je sais qu'une réponse a déjà été cochée, mais pour référence future, vous pouvez toujours utiliser la taille d'écran de l'appareil pour déterminer de quel appareil il s'agit:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    CGSize result = [[UIScreen mainScreen] bounds].size;

    if (result.height == 480) {
        // 3.5 inch display - iPhone 4S and below
        NSLog(@"Device is an iPhone 4S or below");
    }

    else if (result.height == 568) {
        // 4 inch display - iPhone 5
        NSLog(@"Device is an iPhone 5/S/C");
    }

    else if (result.height == 667) {
        // 4.7 inch display - iPhone 6
        NSLog(@"Device is an iPhone 6");
    }

    else if (result.height == 736) {
        // 5.5 inch display - iPhone 6 Plus
        NSLog(@"Device is an iPhone 6 Plus");
    }
} 

else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
       // iPad 9.7 or 7.9 inch display.
       NSLog(@"Device is an iPad.");
}

la source
8

Il suffit d'ajouter le code de l'appareil iPhone 4S à ce fil ...

L'iPhone 4S renverra la chaîne @ "iPhone4,1".

Jason Fuerstenberg
la source
J'ai ajouté ceci au post le plus "relevé"
Besi
6

Que diriez-vous de ce code, si une nouvelle version est sortie, vous vous identifierez avec le dernier appareil connu

- (NSString *)getModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);                              
    if ([sDeviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([sDeviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([sDeviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([sDeviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([sDeviceModel isEqual:@"iPhone3,1"]) return @"iPhone3GS";  //iPhone 4 - AT&T
    if ([sDeviceModel isEqual:@"iPhone3,2"]) return @"iPhone3GS";  //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([sDeviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([sDeviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([sDeviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([sDeviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([sDeviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([sDeviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([sDeviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([sDeviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([sDeviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([sDeviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[sDeviceModel componentsSeparatedByString:@","] objectAtIndex:0];

//If a newer version exist
    if ([aux rangeOfString:@"iPhone"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4"
        if (version >= 4) return @"iPhone4s";

    }
    if ([aux rangeOfString:@"iPod"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version >=4) return @"iPod4thGen";
    }
    if ([aux rangeOfString:@"iPad"].location!=NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version ==1) return @"iPad3G";
        if (version >=2) return @"iPad2";
    }
    //If none was found, send the original string
    return sDeviceModel;
}
Rodrigo
la source
1
Je n'aime pas ce que tu fais. Il renvoie de mauvaises références pour les appareils inconnus. Bonne idée, mais mauvaise mise en œuvre.
Oliver
@Oliver Que proposez-vous? Vous devez imaginer si un nouvel appareil est sorti, vous ne pouvez pas changer le code, et pour moi, cela provoque un bug.
Rodrigo
1
Comment identifier l'iPhone 5 / iPod 5? @ "iPhone5,1" @ "iPod5,1" est-il la bonne façon de l'utiliser?
joe
6

Ajout au code d'Arash, je ne me soucie pas de mon application quel modèle j'utilise, je veux juste savoir quel type d'appareil, donc, je peux tester comme suit:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            NSLog(@"I'm definitely an iPad");
    } else {
    NSString *deviceType = [UIDevice currentDevice].model;
                if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound)
                {
                    NSLog(@"I must be an iPhone");

                } else {
                    NSLog(@"I think I'm an iPod");

                }
}
David DelMonte
la source
5

Sur la base des très bonnes réponses ci-dessus, voici ce que j'ai trouvé. Ceci est très similaire à la réponse de @ Rodrigo, mais répond à la préoccupation de @ Oliver dans le commentaire sur cette réponse. Cela permet également d'inclure la chaîne de modèle dans la chaîne de sortie.

+ (NSString *) deviceModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);      

    return deviceModel;
}

+ (NSString *) deviceName {
    NSString *deviceModel = [DeviceGateway deviceModel];                    

    if ([deviceModel isEqual:@"i386"])      return @"Simulator";  //iPhone Simulator
    if ([deviceModel isEqual:@"iPhone1,1"]) return @"iPhone1G";   //iPhone 1G
    if ([deviceModel isEqual:@"iPhone1,2"]) return @"iPhone3G";   //iPhone 3G
    if ([deviceModel isEqual:@"iPhone2,1"]) return @"iPhone3GS";  //iPhone 3GS
    if ([deviceModel isEqual:@"iPhone3,1"]) return @"iPhone4";    //iPhone 4 - AT&T
    if ([deviceModel isEqual:@"iPhone3,2"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([deviceModel isEqual:@"iPhone3,3"]) return @"iPhone4";    //iPhone 4 - Other carrier
    if ([deviceModel isEqual:@"iPhone4,1"]) return @"iPhone4S";   //iPhone 4S
    if ([deviceModel isEqual:@"iPod1,1"])   return @"iPod1stGen"; //iPod Touch 1G
    if ([deviceModel isEqual:@"iPod2,1"])   return @"iPod2ndGen"; //iPod Touch 2G
    if ([deviceModel isEqual:@"iPod3,1"])   return @"iPod3rdGen"; //iPod Touch 3G
    if ([deviceModel isEqual:@"iPod4,1"])   return @"iPod4thGen"; //iPod Touch 4G
    if ([deviceModel isEqual:@"iPad1,1"])   return @"iPadWiFi";   //iPad Wifi
    if ([deviceModel isEqual:@"iPad1,2"])   return @"iPad3G";     //iPad 3G
    if ([deviceModel isEqual:@"iPad2,1"])   return @"iPad2";      //iPad 2 (WiFi)
    if ([deviceModel isEqual:@"iPad2,2"])   return @"iPad2";      //iPad 2 (GSM)
    if ([deviceModel isEqual:@"iPad2,3"])   return @"iPad2";      //iPad 2 (CDMA)

    NSString *aux = [[deviceModel componentsSeparatedByString:@","] objectAtIndex:0];

    //If a newer version exists
    if ([aux rangeOfString:@"iPhone"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] intValue];
        if (version == 3) return @"iPhone4";
        if (version == 4) return @"iPhone4s";
        return @"Newer iPhone";
    }
    if ([aux rangeOfString:@"iPod"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPod" withString:@""] intValue];
        if (version == 4) return @"iPod4thGen";
        return @"Newer iPod";
    }
    if ([aux rangeOfString:@"iPad"].location != NSNotFound) {
        int version = [[aux stringByReplacingOccurrencesOfString:@"iPad" withString:@""] intValue];
        if (version == 1) return @"iPad3G";
        if (version == 2) return @"iPad2";
        return @"Newer iPad";
    }

    //If none was found, send the original string
    return deviceModel;
}

+ (NSString *) deviceNameWithDeviceModel:(BOOL)shouldIncludeDeviceModel {
    if (shouldIncludeDeviceModel) {
        return [NSString stringWithFormat:@"%@ (%@)", [DeviceGateway deviceName], [DeviceGateway deviceModel]];
    }

    return [DeviceGateway deviceName];
}
benvolioT
la source
Pouvons-nous l'utiliser pour une application AppStore? ce code utilise-t-il une API privée (sysctlbyname)? Tx pour votre aide
fvisticot
1
Pas d'utilisation d'API privées ici. Cela a passé la validation automatisée dans XCode.
benvolioT
D'où obtenez-vous DeviceGateway?
Nizzy
DeviceGateway est le nom de la classe dont les méthodes statiques sont affichées. Donc, la ligne que vous demandez à ce sujet appelle simplement l'autre méthode statique dans le code cité. N'hésitez pas à nommer la classe comme vous le souhaitez.
benvolioT
5

Il y a une bibliothèque assez mature (par moi) pour cela appelée SDVersion . Vous pouvez vérifier le modèle de l'appareil en cours d'exécution, la taille de l'écran et de nombreux autres paramètres. Il prend également en charge OSX.

Exemple:

      // Check for device model
      if ([SDVersion deviceVersion] == iPhone6)
           NSLog(@"You got the iPhone 6. Sweet 🍭!");
      else if ([SDVersion deviceVersion] == iPhone6Plus)
           NSLog(@"iPhone 6 Plus? Bigger is better!");
      else if ([SDVersion deviceVersion] == iPadAir2)
           NSLog(@"You own an iPad Air 2 🌀!");

      // Check for device screen size
      if ([SDVersion deviceSize] == Screen4inch)
           NSLog(@"Your screen is 4 inches");
Sebyddd
la source
comment utiliser en swift?
4
NSString *deviceType = [UIDevice currentDevice].model;
Bhavesh Nayi
la source
4
#import <sys/utsname.h> 

NSString *iPhone = [self iPhoneVersion]; 

-(NSString*)iPhoneVersion
{
   NSString *platform;
   struct utsname systemInfo;
   uname(&systemInfo);
   platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

   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 @"VerizoniPhone 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:@"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;
}
MaxEcho
la source
3

Je suis allé un peu plus loin et j'ai converti le gros bloc "isEqualToString" en une classification de masques de bits pour le type d'appareil, la génération et cet autre qualificatif après la virgule (je l'appelle la sous-génération). Il est encapsulé dans une classe avec un appel singleton SGPlatform qui évite beaucoup d'opérations de chaînes répétitives. Le code est disponible https://github.com/danloughney/spookyGroup

Le cours vous permet de faire des choses comme ceci:

if ([SGPlatform iPad] && [SGPlatform generation] > 3) {
    // set for high performance
}

et

switch ([SGPlatform deviceMask]) {
case DEVICE_IPHONE:
    break;
case DEVICE_IPAD:
    break;
case DEVICE_IPAD_MINI:
    break;
}

La classification des appareils est dans la méthode platformBits. Cette méthode devrait être très familière aux lecteurs de ce fil. J'ai classé les appareils selon leur type et leur génération, car je suis principalement intéressé par les performances globales, mais la source peut être modifiée pour fournir toute classification qui vous intéresse, écran rétine, capacités de mise en réseau, etc.

Dan Loughney
la source
3

L'extrait de code mentionné ci-dessous devrait aider:

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   // iPhone device
 }
 else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
   // iPad device
 }
 else {
  // Other device i.e. iPod
 }
Jayprakash Dubey
la source
2
- (BOOL)deviceiPhoneOriPod
  {
    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType rangeOfString:@"iPhone"].location!=NSNotFound)
      return YES;
    else
      return NO;
  }
Arash Zeinoddini
la source
1

Dutchie432et Brian Robbinsont fourni d'excellentes solutions. Mais il manque encore un modèle, l' iPhone 4 de Verizon . Voici la ligne manquante.

if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4"; //Verizon
Di Wu
la source
1
Fait intéressant, il semble qu'il existe désormais un iPhone3,3?
makdad
@makdad Oui. En fait, certains Verizon iPhone 4 se sont avérés être 3,3.
Di Wu
1
Quelqu'un connaît-il encore l'identifiant de l'iPhone 4S?
stoutyhk
1

Les valeurs possibles de

[[UIDevice currentDevice] model];

sont iPod touch, iPhone, iPhone Simulator, iPad,iPad Simulator

Si vous voulez savoir quel matériel iOSest en train de ruiner sur comme iPhone3, iPhone4, iPhone5etc ci - dessous est le code pour que


REMARQUE: Le code ci-dessous peut ne pas contenir toutes les chaînes de l'appareil, je suis avec d'autres gars qui conservent le même code sur GitHub , veuillez donc prendre le dernier code à partir de là

Objectif-C : GitHub / DeviceUtil

Swift : GitHub / DeviceGuru


#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString*)hardwareDescription {
    NSString *hardware = [self hardwareString];
    if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G";
    if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G";
    if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
    if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI";
    //there are lots of other strings too, checkout the github repo
    //link is given at the top of this answer

    if ([hardware isEqualToString:@"i386"]) return @"Simulator";
    if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";

    return nil;
}

- (NSString*)hardwareString {
    size_t size = 100;
    char *hw_machine = malloc(size);
    int name[] = {CTL_HW,HW_MACHINE};
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    free(hw_machine);
    return hardware;
}
Inder Kumar Rathore
la source
1

Réponse dans Swift 3,

struct DeviceInformation {
    // UIDevice.current.model's value is "iPhone" or "iPad",which does not include details; the following "model" is detailed, such as, iPhone7,1 is actually iPhone 6 plus
    static let model: String = {
        var size = 0
        sysctlbyname("hw.machine", nil, &size, nil, 0)
        var machine = [CChar](repeating: 0,  count: Int(size))
        sysctlbyname("hw.machine", &machine, &size, nil, 0)
        return String(cString: machine)
    }()

    static let uuid = UIDevice.current.identifierForVendor?.uuidString ?? ""
    static let idForAdService = ASIdentifierManager.shared().advertisingIdentifier.uuidString

    static func diviceTypeReadableName() -> String {
        switch model {
        case "iPhone1,1":   return "iPhone 1G"
        case "iPhone1,2":   return "iPhone 3G"
        case "iPhone2,1":   return "iPhone 3GS"
        case "iPhone3,1":   return "iPhone 4"
        case "iPhone3,3":   return "iPhone 4 (Verizon)"
        case "iPhone4,1":   return "iPhone 4S"
        case "iPhone5,1":   return "iPhone 5 (GSM)"
        case "iPhone5,2":   return "iPhone 5 (GSM+CDMA)"
        case "iPhone5,3":   return "iPhone 5c (GSM)"
        case "iPhone5,4":   return "iPhone 5c (GSM+CDMA)"
        case "iPhone6,1":   return "iPhone 5s (GSM)"
        case "iPhone6,2":   return "iPhone 5s (GSM+CDMA)"
        case "iPhone7,2":   return "iPhone 6"
        case "iPhone7,1":   return "iPhone 6 Plus"
        case "iPhone8,1":   return "iPhone 6s"
        case "iPhone8,2":   return "iPhone 6s Plus"
        case "iPhone8,4":   return "iPhone SE"
        case "iPhone9,1":   return "iPhone 7"
        case "iPhone9,3":   return "iPhone 7"
        case "iPhone9,2":   return "iPhone 7 Plus"
        case "iPhone9,4":   return "iPhone 7 Plus"
        case "iPod1,1":     return "iPod Touch 1G"
        case "iPod2,1":     return "iPod Touch 2G"
        case "iPod3,1":     return "iPod Touch 3G"
        case "iPod4,1":     return "iPod Touch 4G"
        case "iPod5,1":     return "iPod Touch 5G"
        case "iPod7,1":     return "iPod Touch 6G"
        case "iPad1,1":     return "iPad 1G"
        case "iPad2,1":     return "iPad 2 (Wi-Fi)"
        case "iPad2,2":     return "iPad 2 (GSM)"
        case "iPad2,3":     return "iPad 2 (CDMA)"
        case "iPad2,4":     return "iPad 2 (Wi-Fi)"
        case "iPad2,5":     return "iPad Mini (Wi-Fi)"
        case "iPad2,6":     return "iPad Mini (GSM)"
        case "iPad2,7":     return "iPad Mini (GSM+CDMA)"
        case "iPad3,1":     return "iPad 3 (Wi-Fi)"
        case "iPad3,2":     return "iPad 3 (GSM+CDMA)"
        case "iPad3,3":     return "iPad 3 (GSM)"
        case "iPad3,4":     return "iPad 4 (Wi-Fi)"
        case "iPad3,5":     return "iPad 4 (GSM)"
        case "iPad3,6":     return "iPad 4 (GSM+CDMA)"
        case "iPad4,1":     return "iPad Air (Wi-Fi)"
        case "iPad4,2":     return "iPad Air (Cellular)"
        case "iPad4,3":     return "iPad Air (China)"
        case "iPad4,4":     return "iPad Mini 2G (Wi-Fi)"
        case "iPad4,5":     return "iPad Mini 2G (Cellular)"
        case "iPad4,6":     return "iPad Mini 2G (China)"
        case "iPad4,7":     return "iPad Mini 3 (Wi-Fi)"
        case "iPad4,8":     return "iPad Mini 3 (Cellular)"
        case "iPad4,9":     return "iPad Mini 3 (China)"
        case "iPad5,1":     return "iPad Mini 4 (Wi-Fi)"
        case "iPad5,2":     return "iPad Mini 4 (Cellular)"
        case "iPad5,3":     return "iPad Air 2 (Wi-Fi)"
        case "iPad5,4":     return "iPad Air 2 (Cellular)"
        case "iPad6,3":     return "iPad Pro 9.7' (Wi-Fi)"
        case "iPad6,4":     return "iPad Pro 9.7' (Cellular)"
        case "iPad6,7":     return "iPad Pro 12.9' (Wi-Fi)"
        case "iPad6,8":     return "iPad Pro 12.9' (Cellular)"
        case "AppleTV2,1":  return "Apple TV 2G"
        case "AppleTV3,1":  return "Apple TV 3"
        case "AppleTV3,2":  return "Apple TV 3 (2013)"
        case "AppleTV5,3":  return "Apple TV 4"
        case "Watch1,1":    return "Apple Watch Series 1 (38mm, S1)"
        case "Watch1,2":    return "Apple Watch Series 1 (42mm, S1)"
        case "Watch2,6":    return "Apple Watch Series 1 (38mm, S1P)"
        case "Watch2,7":    return "Apple Watch Series 1 (42mm, S1P)"
        case "Watch2,3":    return "Apple Watch Series 2 (38mm, S2)"
        case "Watch2,4":    return "Apple Watch Series 2 (42mm, S2)"
        case "i386":        return "Simulator"
        case "x86_64":      return "Simulator"

        default:
            return ""
        }
    }
}
DawnSong
la source
1

Pour une comparaison simple, j'aime toujours la macro:

#define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

C'est simple et facile à utiliser.

Md Milan Mia
la source
0

Je voudrais ajouter que pour récupérer la couleur de la façade et du boîtier de l'appareil, il existe une API privée:

UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString([device.systemVersion hasPrefix:@"7"] ? @"_deviceInfoForKey:" :  @"deviceInfoForKey:");
if ([device respondsToSelector:selector]) {
    NSLog(@"DeviceColor: %@ DeviceEnclosureColor: %@", [device performSelector:selector withObject:@"DeviceColor"], [device performSelector:selector withObject:@"DeviceEnclosureColor"]);
}

J'ai blogué à ce sujet et je fournis un exemple d'application:

http://www.futuretap.com/blog/device-colors/

Ortwin Gentz
la source
0
NSString *deviceType = [[UIDevice currentDevice] systemName];

Je peux vous assurer que celui suggéré ci-dessus fonctionnera dans iOS 7 et supérieur. Je pense que cela fonctionnera également sur iOS 6. Mais je n'en suis pas sûr.

GJDK
la source
0

Vous pouvez vérifier GBDeviceInfo sur GitHub, également disponible via CocoaPods. Il fournit une API simple pour détecter diverses propriétés avec la prise en charge de tous les derniers appareils:

  • Famille d'appareils

[GBDeviceInfo deviceDetails].family == GBDeviceFamilyiPhone;

  • Modèle d'appareil

[GBDeviceInfo deviceDetails].model == GBDeviceModeliPhone6.

Pour plus d'informations, consultez le fichier Lisez - moi .

Maxim Pavlov
la source
0

Pour identifier l'iPhone 4S, vérifiez simplement les points suivants:

var isIphone4S: Bool {

    let width = UIScreen.main.bounds.size.width
    let height = UIScreen.main.bounds.size.height
    let proportions = width > height ? width / height : height / width

    return proportions == 1.5 && UIDevice.current.model == "iPhone"
}
Bartłomiej Semańczyk
la source
0

J'ai créé une extension qui vous permet d'avoir 3 niveaux d'identification:

  1. type d'appareil
  2. type d'iPhone / iPad par pouce
  3. modèle d'appareil

Il se met à jour avec les derniers appareils iOS

UIDeviceDetector

Gabriele Quatela
la source