1

How can I tell if the application is running on iPhone 4 or iPhone 3GS or an iPad?

I want to determine if which hardware I'm using and then provide additional functionality if it's an iPhone 4 (such as using the video light or gyro-sensor).

Any help would greatly be appreciated.

Thanks

PS: I'm looking to determine this programmatically and not determine if based on physical appearance.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
  • This looks to be a duplicate of this SO question: http://stackoverflow.com/questions/2884391/api-to-determine-whether-running-on-iphone-or-ipad – BP. Aug 16 '10 at 13:45
  • The Idion Macro doesn't tell me if it's an iPhone 4 or iPhone 3GS It tells me if I am on an iPad or iPhone or iPod Touch – Cocoa Dev Aug 16 '10 at 14:54
  • Under your project file's Build settings (right click on the project and select Get Info), take a look at Targeted device family. I believe you need to set it to iPhone/iPad (universal binary) to get this to work correctly. – BP. Aug 16 '10 at 17:05

2 Answers2

6

Rather then determining device model you should find and use APIs checking if desired functionality is available. For example hasFlash property of AVCaptureDevice for flash and gyroAvailable property of CMMotionManager for gyroscope.

If your app can run on iOS prior to iOS4 you should also perform extra check if listed above properties and classes are available in run-time.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Sorry, can't make that now - will try to make it later you will still need it – Vladimir Aug 16 '10 at 15:31
  • 1
    Cocoa Dev: Note that Vladimir is referring to the **flash** feature on the camera, not the **Flash Player**, which doesn't exist on the iOS. – Peter Hosey Aug 17 '10 at 08:36
  • Yes I am aware that there is no Adobe Flash available on any iOS device at this time. I also was referring to the LED Flash on the device. – Cocoa Dev Aug 17 '10 at 16:13
5

RE: sample code to detect if a flash is available

   for (AVCaptureDevice *cameraDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
        if ([cameraDevice hasFlash]) {
            NSLog(@" Yay! A flash!"):
        }
    }

But seriously, dude, Vladimir gave you the class name and the method name. If you had opened the documentation, you'd have had figured it out in a jiffy - certainly quicker than waiting on someone to paste you five lines of code.

Svetoslav
  • 156
  • 1
  • 7
  • I can't compile this for the simulator. Anyone else has similar problems? – PEZ Oct 04 '10 at 20:24
  • That actually is a very common issue - in SDK 4 and above (and likely in previous versions, but I haven't used it before that, so I can't say for sure) there is no i386 version of AVFoundation, or at least of parts of it. The only solution I've found is to wrap all AVFoundation code in #if !TARGET_IPHONE_SIMULATOR – Svetoslav Nov 05 '10 at 06:22