【发布时间】:2015-03-18 11:43:05
【问题描述】:
当我使用此代码配置 CLlocationManager 时:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
// Or [self.locationManager requestWhenInUseAuthorization];
} [self.locationManager startUpdatingLocation];
}
并设置位置委托
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
self.textLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
self.textLatitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
不要发送已授权的警报,但 Info.plist 是完整的
当进入设置并更改设置 -> 隐私 -> 本地化并更改“始终”的状态,然后重新启动应用程序时,此状态是干净的。
【问题讨论】:
标签: objective-c cllocationmanager