There is a model property of the device, but it doesn't distinguish between iPhone and iPhone 3G. Is there a better way?
-
3Why do you want to do this? Instead of checking on which platform you are in order to know about the existence of a feature, you should check for the existence of the feature. Saves you some compatibility nightmares in the future. Well, and if you didn't want to do this because of feature-checking, you can ignore this comment :) – OregonGhost Apr 24 '09 at 14:21
-
I want to limit an application from GPS capability if it doesn't have it. All phones have location services and you can set the desired accuracy but cannot tell if you're only able to use cellular location services or GPS. – Apr 24 '09 at 16:47
-
I was thinking the same thing, although as OregonGhost said, it might be better to use the error radius reported by the API instead. Even satellite GPS can give you a large radius if you can't see enough satellites, and sometimes the old iPhones get quite the tight location if they happen to see a good WAP. – Bill K Apr 24 '09 at 18:32
4 Answers
The specifically interesting parts of the project Stephan posted are these:
The string values you're likely to see:
/*
Platforms
iPhone1,1 -> iPhone 1G
iPhone1,2 -> iPhone 3G
iPod1,1 -> iPod touch 1G
iPod2,1 -> iPod touch 2G
*/
How to get one of those values:
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);
You can then check the beginning of platform for @"iPhone"
or @"iPod"
and tell if you have a device with GPS or not. I wouldn't recommend matching the whole string for what you want, because the next time a device comes out (such as this summer, most likely) you won't be able to match it without changing code.
Also, if you haven't seen them, Erica Sadun (the project author) has some excellent articles on iPhone dev at ArsTechnica.com and at least one book about it.
actually they can use the code just to detect a model that doesn't and assume all other models have it
that way they don't have to change the code in the future unless they come out with an iPhone that doesn't have a GPS again...
also--- in response to Oregon Ghost--- there are many features (bluetooth is another I can think of off the top of my head) that the SDK provides no way of detecting....
It seems like you're maybe trying to solve the wrong problem here. You really don't want to know whether you're running on an iPhone 3G or an original iPhone. If you write code that tries to infer whether the device has GPS hardware based on its model, you're going to have to change it when a new model comes out (like an iPod Touch with GPS, or a new netbook product that has GPS, or an iPhone 3G Nano that doesn't have GPS).
It's not clear from your description why you think you need to care whether the device has GPS hardware, or not. You can get location information from CoreLocation, and it includes an accuracy measurement. Just use that, and if the accuracy isn't high enough, inform the user.

- 19,598
- 4
- 47
- 69