检查locationServicesEnabled始终返回YES,无论手动切换开关如何,都要确定是否启用了位置服务

时间:2011-09-02 08:32:45

标签: iphone cllocationmanager

location = [[CLLocationManager alloc] init];
    location.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
    location.distanceFilter = 10 ;
    location.delegate=self;



    locationEnabledBool = [CLLocationManager locationServicesEnabled];

    if (locationEnabledBool ==NO) {
        UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [locationAlert show];
        [locationAlert release];

    }
    else
        [location startUpdatingLocation];

locationEnabledBool的值始终为YES,与是否启用位置服务无关。任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:11)

而不是

if (locationEnabledBool == NO) {
    //give error message
}

尝试

if ( [CLLocationManager locationServicesEnabled] ==NO || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
    //give error message
}

我在链接上找到了这个。

Detecting whether location services are enabled for my app

答案 1 :(得分:0)

在测试此代码时,您需要确保在设备上进行测试,而不仅仅是使用iOS模拟器。

此外,我建议您仔细检查该设备上的设置,以确保设置的第一页上的位置服务显示关闭。< / p>