views:

2401

answers:

5

I am trying to determine if my code is running on an iPhone or an iPhone3G. My first try was to use the UIDevice class in UIKit, but both iPhone and iPhone3G return the same responses:

NSLog([[UIDevice currentDevice] name]); // Name of the phone as named by user
NSLog([[UIDevice currentDevice] uniqueIdentifier]); // A GUID like string
NSLog([[UIDevice currentDevice] systemName]); // "iPhone OS" 
NSLog([[UIDevice currentDevice] systemVersion]); // "2.2.1"
NSLog([[UIDevice currentDevice] model]); // "iPhone" on both devices
NSLog([[UIDevice currentDevice] localizedModel]); // "iPhone" on both devices

Those are the only parameters that UIDevice allows you to query.

I looked a bit in Foundation Framework but have not yet found the appropriate calls.

I'm sure there is some piece of hardware I could query (such as something in location services) but that seems like a hack. Does anyone know a simply way of determining this?

Thanks in advance.

+3  A: 

I'm not an iPhone developer but checking for the underlying platform instead of platform capabilities is almost always the wrong choice. If the iPhone gains the functionality you need, you will still fail to run. There's also a chance you'll make the wrong decision on the next iPhone 4.0 (or whatever it is called).

Steve Rowe
This is purely for logging purposes, specifically logging who is using our app and what kind of platform they are on. So in this case no choice or branch is being made on the platform, just a log. Thanks for the advice though Steve.
Aftermathew
Okay. In that case, checking should be fine.
Steve Rowe
You could use it for performance reasons. There is a big difference in power between iPhone 3G and iPhone 4. You could pack a lot more effects in there for iPhone 4 users.
Jonny
A: 

My guess is that there is functioanlity on the iPhone that is not on the iPhon3G that your software is concerned with. My Suggestion is to query about that specific hardware instead of changing the software behaviour based on the platform.

For example. instead of checking for the availibility of GPS by asking is this an iPhone or iPhone3G query about the availability of the GPS and go from there. That way your code will work on any platform (iphone/ipod touch/iphone 3G)

hhafez
Thanks for the advice hhafez. As mentioned in my comment to Steve Rowe above, this is purely for logging purposes, so really I'm looking to determine what the actual platform is.
Aftermathew
+16  A: 

The iPhone runs OS X. Here's how to determine your hardware platform on a Macintosh desktop. Here's how on an iPhone. It's the same exact thing.

In short, sysctlbyname("hw.machine", str, sz, 0, 0) will write the platform name into str. This happens to be "iPhone1,1" or "iPhone1,2" for the iPhone and iPhone 3G respectively.

ephemient
This is perfect, thanks ephemient.
Aftermathew
FYI, iPhone 4 shows up as iPhone3,1 for those curious since '09.
UltimateBrent
What about iPhone 3GS?
Jonny
@Jonny iPhone 3GS identifies itself as "iPhone2,1", and iPhone 4 identifies itself as "iPhone3,1". You should be able to find this information for yourself on Google.
ephemient
A: 

iPhone first generation has Bluetooth, but for some reason OS 3.0 didn't enable peer-to-peer support on this model. How should one go about testing for specific Bluetooth capabilities without going down the platform ID route?

http://support.apple.com/kb/HT3647

ingulsrud
This is not an **Answer**. Post your own **Question** if you want a question answered.
ephemient
A: 

FWIW, I wrote a comprehensive tutorial on how to determine the device type, from iPhone 3G or 3GS to iPod first or second generation.

You can read more here:

Determine if iPhone is 3G or 3GS / Determine if iPod is First or Second Generation

John