Determine device (iPhone, iPod Touch) with iOS

后端 未结 30 1775
礼貌的吻别
礼貌的吻别 2020-11-21 11:29

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

相关标签:
30条回答
  • 2020-11-21 11:48

    Just adding the iPhone 4S device code to this thread...

    The iPhone 4S will return the string @"iPhone4,1".

    0 讨论(0)
  • 2020-11-21 11:51

    Below mentioned code snippet should help :

     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
       // iPhone device
     }
     else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
       // iPad device
     }
     else {
      // Other device i.e. iPod
     }
    
    0 讨论(0)
  • 2020-11-21 11:52

    Please feel free to use this class (gist @ github)

    CODE REMOVED AND RELOCATED TO

    https://gist.github.com/1323251

    UPDATE (01/14/11)

    Obviously, this code is a bit out of date by now, but it can certainly be updated using the code on this thread provided by Brian Robbins which includes similar code with updated models. Thanks for the support on this thread.

    0 讨论(0)
  • 2020-11-21 11:52

    How about this code, if new version was released, you will identifier with the last know device

    - (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;
    }
    
    0 讨论(0)
  • 2020-11-21 11:52
    NSString *deviceType = [[UIDevice currentDevice] systemName];
    

    I can assure that the above suggested one will work in iOS 7 and above. I believe it will work in iOS 6 too. But am not sure about that.

    0 讨论(0)
  • 2020-11-21 11:53

    Adding to Arash's code, I don't care for my app what model I'm using, I just want to know what kind of device, so, I can test as follows:

        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");
    
                    }
    }
    
    0 讨论(0)
提交回复
热议问题