- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.allowsBackgroundLocationUpdates = true;
[locationManager requestAlwaysAuthorization];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
latitude = locationManager.location.coordinate.latitude;
longitude = locationManager.location.coordinate.longitude;
NSLog(@"New Location. Lat: %f, Long: %f", latitude, longitude);
}
答案 0 :(得分:4)
问题是startUpdatingLocation
异步运行,因此检查授权和调用locationManager:didChangeAuthorizationStatus:
的代码将在用户授权之前执行。这就是它第二次工作的原因,因为他们第一次给了这个许可。您可以做的是在代理上实施startUpdatingLocation
并在其中调用private Bitmap bitmap;
(如果状态已更改为已授权)。
当前授权状态为 kCLAuthorizationStatusNotDetermined,此方法异步运行 并提示用户授予应用程序使用位置的权限 服务