【发布时间】:2015-04-25 22:26:08
【问题描述】:
请不要将其标记为重复,因为我从堆栈溢出中发布的其他人那里获得了帮助。但我仍然面临一些问题。即使在真实设备中也不会调用位置管理器委托。而且它也没有请求许可。
下面是我的代码。
- (IBAction)getUserLocationAction:(id)sender
{
if([CLLocationManager locationServicesEnabled])
{
if(!locationManager)
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
//[locationManager startMonitoringSignificantLocationChanges];
}
else
{
NSLog(@"Location service is not enabled");
}
}
#pragma mark - Location Manager Delegate-
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", locations);
userLocation = [locations lastObject];
NSLog(@"User Current Locatiion\nLat: %+.6f\nLong: %+.6f", [userLocation coordinate].latitude, [userLocation coordinate].longitude);
[locationManager stopUpdatingLocation];
//[locationManager stopMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", [error localizedDescription]);
}
我还在我的 .plist 文件中添加了以下两个键
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app want to use your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app want to use your location</string>
我还添加了框架
我不确定我在哪里做错了。请帮帮我。
更新
首先,我认为这是我的代码的问题,但经过一些研究,它在 iOS7 中按预期工作。代码保持不变,此问题仅在 iOS8 中出现。请让我知道在 iOS8 中工作还需要做哪些更改。
【问题讨论】:
-
当用户接受您的应用使用该位置时,您在哪里处理回调?
-
它没有请求任何许可。
-
在
[locationManager requestWhenInUseAuthorization];这一行设置断点,看是否被调用。 -
是的,它被调用了。
-
那你应该参考属性
self.locationManager,而不是iVarlocationManager
标签: ios iphone delegates core-location cllocationmanager