CLLocation didupdatetolocation没有被调用

时间:2014-10-06 14:35:28

标签: ios objective-c gps location

花了几天时间试图解决这个问题但无法找到任何有效的解决方案。我已经检查了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];

我已经检查过以确保启用了locationServicesEnabled。

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

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

5 个答案:

答案 0 :(得分:6)

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

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

实现此委托方法:

-(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;
    }
}

答案 1 :(得分: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];

并在didUpdateLocations

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

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

    }

答案 2 :(得分:0)

傻傻的我。我将关键字添加到了错误的plist文件中。我现在已经开始工作了。谢谢你的帮助。

答案 3 :(得分:0)

我只花了一整个上午[老实说我是相对新的OOC],无论如何问题......

didUpdateLocation编译,永远不会被调用 didUpdateLocations编译并运行!!

在这种情况下似乎不是答案,但问题在于,这是一个容易犯的错误。使用IOS 8.1.3& Xcode 6.1.1

答案 4 :(得分:0)

只需在Info.plist中添加三个属性。

获取当前位置,如果没有调用didUpdateToLocation方法

String类型的

NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription privacy - location usage description