我在更新到iOS 8后遇到了很多麻烦。以下代码在我更新到iOS和Xcode 6之前有效。代码写在viewDidLoad中:
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
在我的.h文件中,我将locationManager作为属性,同时声明CLLocationManagerDelegate:
@property CLLocationManager *locationManager;
我在
中设置了一个断点-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
但代码从未运行过。
我错过了什么步骤?
编辑:请注意,提示用户允许位置服务的提示永远不会出现。我怀疑这是问题,但在我宣布委托给自己后,我确实请求了服务。
还添加了
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message goes here</string>
但仍然不适合我
答案 0 :(得分:1)
如果您在用户同意使用位置服务器之前启动位置
ios8或更高版本更改为
- (xx)xxxx
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager requestWhenInUseAuthorization];
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
[self.locationManager startUpdatingLocation];
}
}
答案 1 :(得分:0)
您需要在info.plist中添加NSLocationWhenInUseUsageDescription
密钥。 (从我的测试中,它可以是一个空字符串,但密钥必须存在。但不确定Apple是否因为空字符串而无法检查。)
答案 2 :(得分:0)
在开始位置更新之前,您需要使用requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅在前景时的位置)调用CLLocationManager。
Info.plist中还需要NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键,并在提示中显示消息。
答案 3 :(得分:0)
因此,在所有“iOS 8”问题之后,我意识到我的问题是我没有分配并初始化我的locationManager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];