我的应用程序在iphone中打开/关闭GPS位置

时间:2013-03-25 06:33:48

标签: iphone ios gps cllocationmanager

我检查GPS位置on/off是否使用此代码

BOOL servicesEnabled = [CLLocationManager locationServicesEnabled];

在iphone中打开/关闭LocationServices时它可以正常工作,但我为我的应用设置了LocationServices,它只返回servicesEnabledYES,那个时间[CLLocationManager locationServicesEnabled]如何检查{{ 1}}在我的应用程序中 例如:我禁用我的应用位置服务,如Instagram,如何解决此问题以检查iphone或ipad中的特定应用中的locationServicesEnabledenter image description here

1 个答案:

答案 0 :(得分:2)

使用CLLocationManagerDelegate's locationManager: didFailWithError:方法检查kCLErrorDenied错误,看看用户是否拒绝了位置服务。

- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;

    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    // Set a movement threshold for new events.

    locationManager.distanceFilter = 500;

    [locationManager startUpdatingLocation];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations {

    // If it's a relatively recent event, turn off updates to save power

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{

    NSLog(@"%@",error);
}

如果您的应用禁用了位置服务,那么它会给您错误

Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"