CLLocation didupdatetolocation没有被调用

dog*_*tar 6 gps location objective-c ios

花了几天时间试图解决这个问题但无法找到任何有效的解决方案.我已经检查了stackoverflow上的所有帖子,并尝试了所有他们的解决方案,似乎没有任何工作.我也尝试过CLLocation的苹果测试项目,对我来说很好.我使用了苹果测试项目的点点滴滴

https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html

但我的代码根本不起作用.DidupdateToLocation永远不会被调用.

这是我的代码(在viewDidLoad中)

_locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;



// This is the most important property to set for the manager. It ultimately determines how the manager will

// attempt to acquire location and thus, the amount of power that will be consumed.

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;



// Once configured, the location manager must be "started"

//

// for iOS 8, specific user level permission is required,

// "when-in-use" authorization grants access to the user's location

//

// important: be sure to include NSLocationWhenInUseUsageDescription along with its

// explanation string in your Info.plist or startUpdatingLocation will not work.

//

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

    [self.locationManager requestWhenInUseAuthorization];

}

[self.locationManager startUpdatingLocation];



[self performSelector:@selector(stopUpdatingLocationWithMessage:)

           withObject:@"Timed Out"

           afterDelay:30];
Run Code Online (Sandbox Code Playgroud)

我已检查以确保启用locationServicesEnabled.

我已将NSLoationWhenInUseUsageDescription属性添加到info.plist文件中.我需要添加或启用任何服务吗?

我不能因为上帝的爱而弄清楚我做错了什么.有人可以帮我这个.

Dáv*_*zás 6

在iOS8上收到didChangeAuthorizationStatus回调后,你必须调用startUpdatingLocation.

//iOS 8 API change
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
    [self.locationManager requestWhenInUseAuthorization];
}else{
    [self.locationManager startUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)

实现这个委托方法:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied:
        {
            // do some error handling
        }
            break;
        default:{
            [self.locationManager startUpdatingLocation];
        }
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)


Smi*_*ess 6

首先,不要忘记,使用IOS 8你需要做[locationManager requestWhenInUseAuthorization] 加号[locationManager requestAlwaysAuthorization];

_locationManager = [CLLocationManager new];

if(SYSTEM_IS_OS_8_OR_LATER) {
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestAlwaysAuthorization];

}

_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 //and the problem with your code : don't forget to start
_locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

在你的 didUpdateLocations

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

        _currentLocation = [locations objectAtIndex:0]; 
        //do your stuff with the location

    }
Run Code Online (Sandbox Code Playgroud)


dog*_*tar 0

愚蠢的我。我将关键字添加到了错误的 plist 文件中。我现在已经可以使用了。谢谢你们的帮助。