Détecter le type d'appareil

dans mon application (écrite en Objective-C), Comment puis-je détecter si l'appareil est un iPhone, iPad, ou iPhone5 ?

if([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    // [iphone] or [itouch]
} else {
    // [ipad]
}
9
demandé sur Albert Renshaw 2012-10-30 16:54:23
la source

5 ответов

vous pouvez facilement détecter iphone, iphone5 et iPad avec condition ci-dessous (mais pas iTouch! iTouch est traité comme un iPhone avec ce code!):-

 if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
 {
     if ([[UIScreen mainScreen] bounds].size.height == 568)
     {


     }
     else
     {
         //iphone 3.5 inch screen
     }
 }
 else
 {
        //[ipad]
 }

mise à JOUR

vous pouvez également utiliser MACRO ou définir la Variable pour vérifier est que iPhone5,iPhone4 ou iPad comme ci-dessous: -

#define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
#define isiPhone  (UI_USER_INTERFACE_IDIOM() == 0)?TRUE:FALSE

exemple: -

if(isiPhone)
     {
         if (isiPhone5)
         {


         }
         else
         {
             //iphone 3.5 inch screen
         }
     }
     else
     {
            //[ipad]
     }
29
répondu Nitin Gohel 2013-11-14 09:21:42
la source

s'il vous Plaît vérifier ci-dessous de code:

UIDevice_enhanced.h

@interface UIDevice (Enhanced)

typedef enum {
    kUnknownPlatform = 0,
    kiPhone1G,
    kiPhone3G,
    kiPhone3GS,
    kiPhone4,
    kiPhone4Verizon,
    kiPhone4S,
    kiPhone5GSM,
    kiPhone5CDMA,
    kiPhone5CGSM,
    kiPhone5CCDMA,
    kiPhone5SGSM,
    kiPhone5SCDMA,
    kiPhone6,
    kiPhone6P,
    kiPhone6S,
    kiPhone6SP,
    kiPodTouch1G,
    kiPodTouch2G,
    kiPodTouch3G,
    kiPodTouch4G,
    kiPodTouch5G,
    kiPad,
    kiPad2Wifi,
    kiPad2GSM,
    kiPad2CMDA,
    kiPad3Wifi,
    kiPad3GSM,
    kiPad3CMDA,
    kiPad4Wifi,
    kiPad4GSM,
    kiPad4CMDA,
    kiPadAirWifi,
    kiPadAirCellular,
    kiPadMiniWifi,
    kiPadMiniGSM,
    kiPadMiniCDMA,
    kiPadMini2GWifi,
    kiPadMini2GCellular,
    kSimulator
} PlatformType;

- (NSString *) platformName;
- (PlatformType) platform;

@end

UIDevice_enhanced.m

#import "UIDevice_enhanced.h"
#include <sys/utsname.h>

@interface UIDevice (Enhanced)
- (NSString *) platformCode;
@end 


@implementation UIDevice (Enhanced)

// Utility method (private)
- (NSString*) platformCode {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString* platform =  [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    return platform;
}

// Public method to use
- (NSString*) platformName {
    NSString* platform = [self platformCode];

        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:@"iPhone8,2"])    return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";

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:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

return platform;
}

// Public method to use
- (PlatformType) platform {
    NSString *platform = [self platformCode];
    if ([platform isEqualToString:@"iPhone1,1"])    return kiPhone1G;
    if ([platform isEqualToString:@"iPhone1,2"])    return kiPhone3G;
    if ([platform isEqualToString:@"iPhone2,1"])    return kiPhone3GS;
    if ([platform isEqualToString:@"iPhone3,1"])    return kiPhone4;
    if ([platform isEqualToString:@"iPhone3,2"])    return kiPhone4Verizon;
    if ([platform isEqualToString:@"iPhone4,1"])    return kiPhone4S;
    if ([platform isEqualToString:@"iPhone5,1"])    return kiPhone5GSM;
    if ([platform isEqualToString:@"iPhone5,2"])    return kiPhone5CDMA;
    if ([platform isEqualToString:@"iPhone5,3"])    return kiPhone5CGSM;
    if ([platform isEqualToString:@"iPhone5,4"])    return kiPhone5CCDMA;
    if ([platform isEqualToString:@"iPhone6,1"])    return kiPhone5SGSM;
    if ([platform isEqualToString:@"iPhone6,2"])    return kiPhone5SCDMA;
    if ([platform isEqualToString:@"iPhone7,1"])    return kiPhone6P;
    if ([platform isEqualToString:@"iPhone7,2"])    return kiPhone6;
    if ([platform isEqualToString:@"iPhone8,2"])    return kiPhone6SP;
    if ([platform isEqualToString:@"iPhone8,1"])    return kiPhone6S;

    if ([platform isEqualToString:@"iPod1,1"])      return kiPodTouch1G;
    if ([platform isEqualToString:@"iPod2,1"])      return kiPodTouch2G;
    if ([platform isEqualToString:@"iPod3,1"])      return kiPodTouch3G;
    if ([platform isEqualToString:@"iPod4,1"])      return kiPodTouch4G;
    if ([platform isEqualToString:@"iPod5,1"])      return kiPodTouch5G;

    if ([platform isEqualToString:@"iPad1,1"])      return kiPad;
    if ([platform isEqualToString:@"iPad2,1"])      return kiPad2Wifi;
    if ([platform isEqualToString:@"iPad2,2"])      return kiPad2GSM;
    if ([platform isEqualToString:@"iPad2,3"])      return kiPad2CMDA;
    if ([platform isEqualToString:@"iPad2,4"])      return kiPad2Wifi;
    if ([platform isEqualToString:@"iPad2,5"])      return kiPadMiniWifi;
    if ([platform isEqualToString:@"iPad2,6"])      return kiPadMiniGSM;
    if ([platform isEqualToString:@"iPad2,7"])      return kiPadMiniCDMA;
    if ([platform isEqualToString:@"iPad3,1"])      return kiPad3Wifi;
    if ([platform isEqualToString:@"iPad3,2"])      return kiPad3CDMA;
    if ([platform isEqualToString:@"iPad3,3"])      return kiPad3GSM;
    if ([platform isEqualToString:@"iPad3,4"])      return kiPad4Wifi;
    if ([platform isEqualToString:@"iPad3,5"])      return kiPad4GSM;
    if ([platform isEqualToString:@"iPad3,6"])      return kiPad4CDMA;
    if ([platform isEqualToString:@"iPad4,1"])      return kiPadAirWifi;
    if ([platform isEqualToString:@"iPad4,2"])      return kiPadAirCellular;
    if ([platform isEqualToString:@"iPad4,4"])      return kiPadMini2GWifi;
    if ([platform isEqualToString:@"iPad4,5"])      return kiPadMini2GCellular;

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

    return kUnknownPlatform;
}

@end

Espérons que cela fonctionne pour vous...

11
répondu iCreative 2016-02-10 23:55:03
la source
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if ([[ UIScreen mainScreen ] bounds ].size.height == 568 ) {
nibName = [NSString stringWithFormat:@"%@_568", nibName];

}

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
nibName = [NSString stringWithFormat:@"%@_iPad", nibName]; {

}

if (self = [super initWithNibName:nibName bundle:nibBundle]) {
}

return self;
}

le premier rapport vérifie le temps l'appareil que vous avez A l'écran iPhone 5.

le deuxième contrôle météo vous utilisez un iPad.

le troisième renvoie le xib normal pour l'iPhone 4s et prior.

s'il vous plaît noter, pour faire ce travail, vous aurez à créer 3 XIB différents, tous avec un suffixe différent.

ViewController.xib

ViewController_568.xib

ViewController_iPad.xib

1
répondu Sam Guichelaar 2013-07-17 12:57:13
la source

voici un tas de constantes que vous pouvez coller dans votre fichier PCH et utiliser tout au long de votre application qui vous donnent beaucoup de choses différentes que vous pouvez tester pour. (Widescreen ici signifie iPhone 5 "écran plus grand")

#ifndef IS_WIDESCREEN
#define IS_WIDESCREEN   (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON)
#endif

#ifndef IS_IPHONE
#define IS_IPHONE       ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
#endif

#ifndef IS_IPOD
#define IS_IPOD         ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
#endif

#ifndef IS_IPAD
#define IS_IPAD         ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
#endif

#ifndef IS_IPHONE5
#define IS_IPHONE5      (IS_WIDESCREEN && IS_IPHONE)
#endif

#ifndef IS_IPOD5
#define IS_IPOD5        (IS_WIDESCREEN && IS_IPOD)
#endif

Vous pouvez l'utiliser comme ceci:

if(IS_IPHONE5){
    // load iPhone5 code/nibs
}

if(IS_WIDESCREEN){
    // load 4" screen stuff here
}
0
répondu Thomas Clayson 2013-07-17 12:49:56
la source
if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"]) {//iPhone... (not iTouch or iPad)



if ([[UIDevice currentDevice].model hasPrefix:@"iPad"]) {//iPad...



if (([[UIDevice currentDevice].model hasPrefix:@"iPhone"]) && ([UIScreen mainScreen]bounds].size.height == 568)) {//iPhone5...





Et aussi, juste pour la référence... iTouch est ceci:

if ([[UIDevice currentDevice].model hasPrefix:@"iPod"]) {//iTouch
0
répondu Albert Renshaw 2013-11-15 01:48:37
la source

Autres questions sur