Determine device (iPhone, iPod Touch) with iOS

后端 未结 30 2163
礼貌的吻别
礼貌的吻别 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: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;
    }
    

提交回复
热议问题