English 中文(简体)
Detect GPS Hardware in iphone
原标题:

I want to know how to detect GPS HardWare in present in Iphone or not

最佳回答

Even though you can not determine this using the SDK, you may exploit your knowledge of current models. If you limit yourself to iPhone 3G, iPhone 3GS and iPod Touch 2nd generation (I do not personally support older iPhone edge or iPod Touch 1st generation), then you already know that the iPhone models ship with a real AGPS unit while the iPod Touch 2nd generation can use - if and when available - geotagged wifi hot spots. You may then use the following method to determine the model you are running on:

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

The method returns @"iPhone2,1" for the 3GS, @"iPhone1,2" for the 3G and @"iPod2,1" for the ipod touch 2nd generation; you call it as follows

NSString *deviceModel = [[self deviceModel] retain];

and use it as follows:

if([self.deviceModel isEqualToString:@"Pod2,1"]){
    // no real GPS unit available 
    // no camera available
    ...
}
问题回答

You can t detect the hardware, at least not through the official SDK. You can, however, interact with it (namely, getting information out of it) from your application (via the CoreLocation framework). And this works in every device ever shipped since the release of iPhone OS 2.0 (even iPod touches without a real GPS inside).

The best I found is to leverage the fact that CLLocation.verticalAccuracy is only set for GPS equipped devices. Of course this means that GPS availability can only be determined after the first location update.

From Apple s documentation:

Determining the vertical accuracy requires a device with GPS capabilities. Thus, on some earlier iOS-based devices, this property always contains a negative value.

Update: Sometimes, Core Location sends CLLocation objects with verticalAccuracy = -1 on an iPhone 4 as well. Probably it s doing that when no GPS fix is available and Core Location resorts to cell or Wi-Fi positioning. So the fact you re getting -1 doesn t necessarily mean you don t have GPS. Only if you re getting a positive value you know for sure, you do have GPS.

In order to avoid asking for GPS permissions just to know whether or not the GPS exists, I use a little trick. Since all the devices with a real GPS also have 3G I just check if the device has a phone operator.

//Use classLoaders to avoid crashes on iPhone OS 3.x
id netInfo = [[NSClassFromString(@"CTTelephonyNetworkInfo") alloc] init];
if (netInfo) 
{
            id carrier = [netInfo subscriberCellularProvider];

            if ([[carrier carrierName] length] <=0)
            {
                    //NO operator=>NO 3G and no real GPS  
            }
}
[netInfo release];

Of course this may not be 100% future proof but it works well at this point. I use it in my Battery HD app in order to hide GPS estimates so I obviously don t want to ask the user for authorizations for that.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签