【发布时间】:2017-01-13 14:57:05
【问题描述】:
我的课堂上有这个代码ViewController:
CLLocationManager *locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Did finish with error - %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"did Update Location - %@", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
但我没有获得允许访问的位置或弹出窗口。
我正在使用核心位置框架。
点击按钮时,我在标签中打印纬度、经度和地址。
我正在模拟器上测试这个。
【问题讨论】:
-
我提醒您,在plist中添加授权请求并检查您的设备可以获得。
-
你的.plist中添加授权请求说明了吗?
-
KAR 检查上面的链接它为您提供解决方案
-
我在 info plist 文件中添加了。谢谢@user3182143
标签: ios objective-c xcode core-location