iPhone 4 detection... on simulator
I need to detect if the user is using an iPhone 4, but I need this to work on the simulator (cause Apple forgot my country and there's no sign of iPhone 4 here soon).
I found this http://www.clintharris.net/2009/iphone-model-via-sysctlbyname/
but running this on the simulator it does not detect the correct version. I mean, Xcode 3.2.3 has two开发者_JS百科 simulators (3G/3GS and 4). I was expecting the detection method to tell me the correct version I am using... but instead it tells me "iphone simulator"...
Is there any way to do that? thanks.
You don't need to detect the system version in your case.
Suppose an image is named foo.png
, then you just need to add
foo~ipad.png
for iPadfoo@2x~iphone.png
for iPhone 4
and load the image with [UIImage imageNamed:@"foo.png"]
. See the iPhone Application Programming Guide for detail.
I think it would be better to check for the feature you require, rather than a specific device. This is certainly what Apple recommends, as it gives you insurance when new devices roll around!
In this case, would it work to check the size of the screen, and use that to determine if you need to scale your image?
[[UIScreen mainScreen] bounds]
Hardware Availability and State
If a hardware feature (for example, a gyroscope) is not available on a device, calling a start method related to that feature has no effect. You can find out whether a hardware feature is available or active by checking the appropriate property; for example, for gyroscope data, you can check the value of the gyroAvailable or gyroActive properties.
Use
@property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable
of class CMMotionManager.
See
@property (nonatomic, readonly, retain) NSString *systemVersion;
// It equal to @"4.0" on iOS 4.0
and
@property (nonatomic, readonly, retain) NSString *model;
// Possible examples of model strings are @”iPhone” and @”iPod touch”
of class UIDevice.
From Erica Sudan:
- (NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
/*
Possible values:
"iPhone1,1" = iPhone 1G
"iPhone1,2" = iPhone 3G
"iPhone2,1" = iPhone 3GS
"iPhone3,1" = iPhone 4
"iPod1,1" = iPod touch 1G
"iPod2,1" = iPod touch 2G
*/
NSString *platform = [NSString stringWithCString:machine];
free(machine);
return platform;
}
Or, if you just need to detect if it's a high res screen, you can use:
UIScreen *screen = [UIScreen mainScreen];
BOOL isHighRes;
if ([screen respondsToSelector:@selector(scale)]) {
isHighRes = ([screen scale] > 1);
} else {
isHighRes = NO;
}
Mike, you can know if the user is using an iPhone 4 by using preprocessor instructions. For example:
#ifdef __IPHONE_4_0
// Do some work for iPhone 4 device
#else
// Do some work for non iPhone 4 device
#endif
I hope it can help you.
精彩评论