CLLocation的didupdatetolocation方法未被调用

6
花了几天时间尝试解决这个问题,但无法找到任何有效的解决方案。我已经检查了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文件中。是否还需要添加其他属性或启用任何服务?

我真的不知道自己做错了什么。有人能帮我吗?


顺便问一下,授权警报弹出了吗? - Dávid Kaszás
授权警报未弹出。 - dogwasstar
1
那么我猜didChangeAuthorizationStatus回调函数也没有被调用,对吗?应用程序的位置服务已启用吗?(设置/隐私/位置服务)你确定已经将正确的密钥添加到你的.plist文件中了吗? - Dávid Kaszás
嘿,戴维,是的,我已经检查过我正在将正确的密钥添加到 plist 中。仍然无法使其工作。必须搁置一段时间。今天要尝试修复它。 - dogwasstar
5个回答

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

6

首先,别忘了,在iOS8中您需要执行[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

    }

添加 requestWhenInUseAuthorizationrequestAlwaysAuthorization 两者都是解决方案。 - Vaibhav Saran

0
傻我了。我把关键词添加到了错误的plist文件中。现在我已经搞定了。谢谢你们的帮助。

0

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

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

NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription privacy - location usage description类型为字符串。


0

我刚刚花了整个早上在这个问题上 [老实说,我是相对新的OOC],无论如何,问题是..

didUpdateLocation编译通过但从未被调用 didUpdateLocations编译通过并且可以工作!!

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接