views:

202

answers:

3

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.

+1  A: 

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
The Idion Macro doesn't tell me if it's an iPhone 4 or iPhone 3GSIt tells me if I am on an iPad or iPhone or iPod Touch
Cocoa Dev
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
+3  A: 

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
Do you have some sample code to show if it has Flash?
Cocoa Dev
Sorry, can't make that now - will try to make it later you will still need it
Vladimir
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
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
+4  A: 

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.

Lurch
I can't compile this for the simulator. Anyone else has similar problems?
PEZ