views:

149

answers:

6

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 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.

A: 

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.

Sebastian
The editor removes the # sign, at the start of the ifdef, else and enfid you need to add a #.
Sebastian
thanks, but this will not detect the device at run time. I need the detection to be on-the-fly, not preprocessed.
Digital Robot
A: 

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.

nepo
He doesn't want to detect the software version, but the hardware version.
calmh
Ok, i see now...
nepo
+1  A: 

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]
TheNextman
this does not work anymore. iPhone 4 will report 480x320 !!! Apple wants us to suffer.
Digital Robot
Well that sucks :(
TheNextman
@Mike - Actually, Apple did it this way so that we don't have to completely rewrite our existing iPhone applications to support the new display. Instead, they've provided a scale property on UIScreen and elsewhere that indicates how the coordinate space will be scaled to fit the new high-resolution display, as well as the @2x extension for high-resolution images that transparently swaps them in behind the scenes. This is about as easy as they could make it.
Brad Larson
+1  A: 

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.

nepo
This tells him if the device has a gyro. Right now the only Apple device with a gyro is the iPhone 4 - but about next year when, for example, iPhone 5 releases with a gyro and a different shaped screen that requires him to process the images differently? It doesn't seem like a very pragmatic solution...
TheNextman
+4  A: 

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 iPad
  • foo@2x~iphone.png for iPhone 4

and load the image with [UIImage imageNamed:@"foo.png"]. See the iPhone Application Programming Guide for detail.

KennyTM
thanks. I think this is the best method for my case!
Digital Robot
Also, I recommend watching the WWDC 2010 video session 134 - "Optimize your iPhone App for the Retina Display". All of this is explained in detail there.
Brad Larson
A: 

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;
}
Dave Wood