I want to get iphone device type if it is iphone 2 or 3G or 4 in xcode 4.0.
Is there any way to get it?
If it 开发者_StackOverflow社区is, please tell me.
I found some new algorithm to get device type and iOS version.
#include <sys/sysctl.h>
#include <sys/utsname.h>
- (NSString *) platformString{
NSLog(@"[UIDevice currentDevice].model: %@",[UIDevice currentDevice].model);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
NSLog(@"[UIDevice currentDevice].batteryLevel: %f",[UIDevice currentDevice].batteryLevel);
struct utsname systemInfo;
uname(&systemInfo);
NSLog(@"[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]: %@",[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
NSString *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,2"]) return @"iPhone 4 CDMA";
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,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 (Cellular)";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (Cellular)";
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 (Cellular)";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return @"Unknown";
}
Actually, the UIDevice
class does not have a method platformString
. With undocumented methods your app will get rejected by Apple.
[[UIDevice currentDevice] model] // e.g. "iPod touch"
will do the trick.
Caleb is right, you shouldn't check for device type, but for functionality. For example, you can check whether the device supports multitasking like so:
UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
// ...code to be executed if multitasking is supported.
// respondsToSelector: is very useful
}
If you must, you can check the iOS version, but know this is not a substitute for checking whether an object respondsToSelector:
.
#define IOS4_0 40000
// You'd probably want to put this in a convenient method
NSArray *versions = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
NSInteger major = [[versions objectAtIndex:0] intValue];
NSInteger minor = [[versions objectAtIndex:1] intValue];
NSInteger version = major * 10000 + minor * 100;
if (version >= IOS4_0) {
// ...code to be executed for iOS4.0+
}
As far as I know, there is no documented way to check the device model.
I have added a static method to my project to check the device type: iPad, iPhone4(or less), and iPhone5 to handle the different screen sizes.
+ (DeviceType) currentDeviceType
{
DeviceType device = DEVICE_TYPE_IPAD ;
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
{
if( [[UIScreen mainScreen] bounds].size.height >= 568 || [[UIScreen mainScreen] bounds].size.width >= 568 )
{
device = DEVICE_TYPE_IPHONE5 ;
}
else
{
device = DEVICE_TYPE_IPHONE4 ;
}
}
return device ;
}
You can also use the type UIUserInterfaceIdiomPad.
From what I can tell, the iPod and iPhone are intended to be treated equally. But if you need to determine if it is an iPod, check to see if it makes phone calls.
Check this out https://github.com/erica/uidevice-extension/
[[UIDevice currentDevice] platformString] //@"iPhone 4"
This post helped me to distinguish between iPhone/iPad types:
You can get the model type (iPhone, iPad, iPod) as shown above:
[[UIDevice currentDevice] model]
And to further specify you can import and find the specific model type returned as char array by the following code:
struct utsname u;
uname(&u);
char *type = u.machine;
And convert it to NSString by:
NSString *strType = [NSString stringWithFormat:@"%s", type];
The usual advice is to not worry about what kind of device you're running on, but instead to test for the features you need. If you look at the device type, you're bound to make assumptions that are incorrect. The versions of each model sold in different markets may vary, for example, and new devices may come along that report the same type but have different features. So, test for features rather than model.
You could define some macros extending off of @csb 's answer to this thread.
#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width == 480 || [[UIScreen mainScreen] bounds].size.height == 480)
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width == 568 || [[UIScreen mainScreen] bounds].size.height == 568)
#define IS_IPAD ([[UIScreen mainScreen] bounds].size.width == 768 || [[UIScreen mainScreen] bounds].size.height == 768)
精彩评论