2013-01-17 12 views
9

ho trovato le soluzioni da qui: Determine device (iPhone, iPod Touch) with iPhone SDKPer rilevare IOS tipo di dispositivo

Dal link, si suggerisce di utilizzare la libreria https://gist.github.com/1323251

Ma ovviamente la biblioteca è piuttosto datata. Non sono riuscito a trovare l'iPhone 5 e il nuovo iPad e così via nell'elenco.

Qualcuno sa come posso trovare l'elenco completo e aggiornato?

Grazie mille.

+2

assegno che http://parasjoshi3.blogspot.ro/2013/01/get-device-model-or-device-type-and.html – TonyMkenu

risposta

30

si può facilmente rilevare iphone, iphone5 e iPad con sotto condizione: -

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


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

la mia risposta: - Detect device type

+8

Si prega di non confrontare ' float's usando '==', non è sicuro. È meglio usare i limiti di '[[UIScreen mainScreen]] .size.height> 567'. –

+0

Ci sono voci sul prossimo iPod con schermo da 5 ", quindi se vuoi creare un'app per il futuro, questo causerà un piccolo problema. – amar

+0

@ H2CO3 è corretto! O semplicemente lo confronta con float letterale, [[UIScreen mainScreen] bound] .size altezza == 568.0f – sleepwalkerfx

3

utilizzare il seguente codice:

#import <sys/utsname.h> 

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

    if ([temp rangeOfString:@"iPod"].location != NSNotFound) 
    { 
     return @"iPod"; 
    } 

    if ([temp rangeOfString:@"iPad"].location != NSNotFound) 
    { 
     return @"iPad"; 
    } 

    if ([temp rangeOfString:@"iPhone"].location != NSNotFound) 
    { 
     return @"iPhone"; 
    } 

    return @"Unknown device"; 
} 
+2

Invece di 'if (! (Stuff == bar)), usa il più leggibile 'if (stuff! = bar)'. Per favore! –

+0

Scusa il mio cattivo si prenderà cura del futuro – amar

2

È possibile utilizzare il seguente codice

if(screenSize.width==2048 && screenSize.height==1536) 
{ 
    LetterParams.DeviceType=1;//IPadRetina 
} 
else if(screenSize.width==2048/2 && screenSize.height==1536/2) 
{ 
    LetterParams.DeviceType=2;//IPad Non-Retina 
} 
else if(screenSize.width==1136 && screenSize.height==640) 
{ 
    LetterParams.DeviceType=3;//IPhoneRetina 
} 
else 
{ 
    LetterParams.DeviceType=4;//IPhone & Ipod 
} 
+0

Confrontare i valori float è abbastanza pericoloso. A seconda dell'umore della CPU, scoprirai che otterrai un sacco di falsi negativi in ​​quelle condizioni, portandoli a entrare in modo errato nel 'altro'. – mylogon

1

Ecco un metodo che è venuto fuori che si concentra su dispositivi chiave per le funzioni di misurazione dello schermo. È un modo rapido per determinare ciò di cui hai bisogno. Questo rileverà fino a iPhone 5 e 5 gen. IPod touch.

typedef enum{ 
    iPadRetina,iPadNoRetina,iPhoneiPod35InchRetina,iPhoneiPod35InchNoRetina,iPhoneiPod4InchRetina}DeviceType; 

-(void)yourCustomFunctionThatNeedsToKnowDeviceType 
{ 
    NSLog(@"device type = %i",[self getDeviceType]); 

    switch ([self getDeviceType]) 
    { 
     case iPadRetina: 
     { 
      NSLog(@"This device is one of the following: iPad 3, iPad 4"); 
      break; 
     } 
     case iPadNoRetina: 
     { 
      NSLog(@"This device is one of the following: iPad 1, iPad 2, iPad mini"); 
      break; 
     } 
     case iPhoneiPod35InchRetina: 
     { 
      NSLog(@"This device is one of the following: iPhone 4/4S or iPod Touch 4th Generation"); 
      break; 
     } 
     case iPhoneiPod35InchNoRetina: 
     { 
      NSLog(@"This device is one of the following: iPhone 3G/3GS or iPod Touch 3rd Generation"); 
      break; 
     } 
     case iPhoneiPod4InchRetina: 
     { 
      NSLog(@"This device is one of the following: iPhone 5 or iPod Touch 5th Generation"); 
      break; 
     } 
    } 
} 

-(int)getDeviceType 
{ 
    // Get the ratio of the device's screen (height/width) 
    CGFloat screenRatio = [UIScreen mainScreen].bounds.size.height/[UIScreen mainScreen].bounds.size.width; 

    // Initialize return value to negative value 
    DeviceType type = -1; 
    if(screenRatio > 1.5) 
    { 
     /* 
      4.0-Inch Screen 
      This implies that the device is either an iPhone 5 or a 5th generation iPod 
      Retina display is implicit 
     */ 
     type = iPhoneiPod4InchRetina; 
    } 
    else 
    { 
     /* 
      Device must be iPad 1/2/3/4/mini or iPhone 4/4S or iPhone 3G/3GS 
     */ 

     // Take a screenshot to determine if the device has retina display or not 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0); 
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *scaleCheckImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
     { 
      /* 
       Device must be iPad 1/2/3/4/mini 
      */ 
      if(scaleCheckImage.scale == 1) 
      { 
       // iPad 1/2/mini (No Retina) 
       type = iPadNoRetina; 
      } 
      else if(scaleCheckImage.scale == 2) 
      { 
       // iPad 3/4 (Retina) 
       type = iPadRetina; 
      } 
     } 
     else 
     { 
      /* 
       Device must be iPhone 4/4S or iPhone 3G/3GS or iPod Touch 3rd Generation or iPod Touch 4th Generation 
      */ 
      if(scaleCheckImage.scale == 1) 
      { 
       // iPhone 3G/3GS or iPod Touch 3rd Generation (No Retina) 
       type = iPhoneiPod35InchNoRetina; 
      } 
      else if(scaleCheckImage.scale == 2) 
      { 
       // iPhone 4/4S or iPod Touch 4th Generation (Retina) 
       type = iPhoneiPod35InchRetina; 
      } 
     } 
    } 
    return type; 
} 
11

Questo funziona bene:

if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { 
    NSLog(@"IPAD"); 
}else{ 
    NSLog(@"IPHONE"); 
} 
+3

Questa risposta dovrebbe essere in cima con tutto punteggio! –

1

Sono con altri ragazzi sono il mantenimento del codice su GitHub quindi si prega di prendere l'ultimo codice da lì. Aggiungiamo continuamente nuovi dispositivi nell'elenco.

Objective-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; 
} 
1
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
{ 
    NSLog(@"All iPads"); 
} 
else 
{ 
    else if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) 
    { 
     if(screenHeight > 480 && screenHeight < 667) 
     { 
      NSLog(@"iPhone 5/5s/6"); 
     } 
     else if (screenHeight > 480 && screenHeight < 736) 
     { 
      NSLog(@"Other iPhones Resizable"); 
     } 
     else if (screenHeight > 480) 
     { 
      NSLog(@"iPhone 6 +"); 
     } 
     else 
     { 
      NSLog(@"iPhone 4s and others"); 
     } 
    } 
5

Basta aggiungere al @Mohammad Kamran Usmani risposta. Tipi di iPhone più specifico:

@import UIKit; 

//Check which iPhone it is 
double screenHeight = [[UIScreen mainScreen] bounds].size.height; 
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
{ 
    NSLog(@"All iPads"); 
} else if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) 
{ 
    if(screenHeight == 480) { 
     NSLog(@"iPhone 4/4S"); 
     smallFonts = true; 
    } else if (screenHeight == 568) { 
     NSLog(@"iPhone 5/5S/SE"); 
     smallFonts = true; 
    } else if (screenHeight == 667) { 
     NSLog(@"iPhone 6/6S"); 
    } else if (screenHeight == 736) { 
     NSLog(@"iPhone 6+, 6S+"); 
    } else { 
     NSLog(@"Others"); 
    } 
} 
Problemi correlati