代码不进入DidupdateLocation()方法,iOS,目标-c,进入didfailwithError()

时间:2016-05-06 07:54:05

标签: ios objective-c xcode

这是我的代码:

if (!self.locationManager) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy =      kCLLocationAccuracyNearestTenMeters;
    }


if([CLLocationManager locationServicesEnabled]){
        [self findCurrentLocation];
        }

-(void)findCurrentLocation {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {

        [self.locationManager performSelector:@selector(requestAlwaysAuthorization)];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.isLocationEnabled=YES;
    self.currentLocation = newLocation;

    if (currentLocation!= nil) {

        latitudeValue = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
        longitudeValue = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];

    }
    [self.locationManager stopUpdatingLocation];
}



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if([error code] == kCLErrorDenied)
    {
        [locationManager stopUpdatingLocation];
    }
    self.isLocationEnabled=NO;
}

我没有收到任何错误,但是didUpdateToLocation从未被调用,它将进入didFailWithError()

我获得纬度和经度的空值。请让我知道如何获得价值。

1 个答案:

答案 0 :(得分:0)

来自iOS8及以上版本的

Info.plist添加密钥,并请求位置管理员授权启动。

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

您需要请求相应位置方法的授权。

[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]

请阅读此文档:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/