在 requestAlwaysAuthorization 之后立即使用 CLLocationManager

Using CLLocationManager right after requestAlwaysAuthorization

我有一个关于 CLLocationManager 的问题。在请求许可后,我想立即使用 locationServices。 我在 viewDidLoad: 中的代码如下所示:

if (![CLLocationManager locationServicesEnabled])
{
    [self locationServicesAreNotEnabled];
}
else
{
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    if (authStatus == kCLAuthorizationStatusDenied || authStatus == kCLAuthorizationStatusRestricted)
    {
        NSLog(@"Location Services unavailable denied/restricted");
    }
    else if (authStatus == kCLAuthorizationStatusNotDetermined)
    {
        [_locationManager requestAlwaysAuthorization];
    }

    if (authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        [self startStandardUpdates];
    }
}

`

代码似乎到达了 [_location Manager requestAlwaysAuthorization] 行;然后,不等待用户的回答,继续执行下一个 if 语句。 我如何才能等待用户的响应,然后才调用

if (authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse)

?我可以通过使用块来实现吗? 谢谢

您需要实施 locationManager:didChangeAuthorizationStatus:。一旦你获得授权就会调用它,然后你可以立即继续使用位置管理器。

你其实可以预先把一个块(说你一获得授权就想做什么)存储为一个属性,这样当locationManager:didChangeAuthorizationStatus:被调用时,你可以查看您是否存储了该块,如果存储了,则调用它。