views:

5021

answers:

6

I am making a game that utilizes the peer-to-peer bluetooth capabilities of the iPhone (and probably the iPod touch 2nd generation). However, to stop the users from trying to play a multiplayer on an iPod 1st gen and iPhone 2G I need to check for the specific device model.

[[UIDevice currentDevice] model] will only tell me if the device is an "iPhone" or an "iPod touch". Is there a way to check for the specific device model, like: "iPhone 3GS", "iPod touch 1st generation" or something.

EDIT:

There is a category to UIDevice (I think it's created by Erica Sadun, I don't take credit for it) that uses the following code to get the specific device model:

#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (Hardware)

/*
 Platforms
 iPhone1,1 -> iPhone 1G
 iPhone1,2 -> iPhone 3G 
 iPod1,1   -> iPod touch 1G 
 iPod2,1   -> iPod touch 2G 
*/

- (NSString *) platform
{
  size_t size;
  sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
  free(machine);
  return platform;
}

This works and apps using this have been lately approved in the AppStore.

+2  A: 

Dr. Touch covered this recently. The code he posted will determine all three different iPhones and the two iTouch devices.

Mr. Matt
A: 

In this SO question, a link was posted to this website. HTH

drvdijk
+11  A: 

You can get the device model number using uname from sys/utsname.h. For example:

#import <sys/utsname.h>

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

The result should be:

@"i386"      on the simulator
@"iPod1,1"   on iPod Touch
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPad1,1"   on iPad
@"iPhone3,1" on iPhone 4.
Will Harris
Thanks for the reply. That did it!Just a correction: iPhone 3GS is "iPhone2,1".Cheers
Dimitris
Thanks. I've updated the answer with the result for iPhone 3GS.
Will Harris
What is the systemInfo for iPhone 2 gen? Is it the same as iPhone 3G?
iPhoney
@iPhoney The original 2G iPhone is @"iPhone1,1".
Will Harris
Anyone know what all of the new devices spit out? iPad? iPod Touch 2010?
jtalarico
@jtalarico I've added the string for iPad. I don't have access to an iPod Touch 2010.
Will Harris
A: 

This is not in API will this code be accepted by apple review process?

Yes. They don't see your code anyway ad I am sure this is actually encouraged (making sure the device is correct)
Dimitris
A: 

Hi, this code works great!

But with Apple's new static analysis tool, will this pass the app store review process though?

Has anyone figure out a way to do it using only public apis? Right now we can only distinguish iPhone 3Gs by testing for the availability of a video camera, but there is there a way to differentiate between iPhone 2G and iPhone 3G.

Thanks!

Annabel
Please see my edit in my initial post that shows you code that definitely passes Apple's approval process today.
Dimitris
Thanks for sharing Dimitris, you're awesome!So it appears that Apple's static analysis tool does not pick up this usage of unpublished API or they just choose to ignore it.Either way this is good news, but apple should provide a documented and supported way to do this.Thanks!
Annabel
+3  A: 

Most complete UIDevice (Hardware) category probably is http://github.com/erica/uidevice-extension/ (by Erica Sadun):

[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
madmw