Ios 获取当前位置时遇到的问题

Ios 获取当前位置时遇到的问题,ios,objective-c,ios7,cllocationmanager,cllocation,Ios,Objective C,Ios7,Cllocationmanager,Cllocation,我想获取设备的当前位置。代码正常工作。如果用户未更改位置服务的应用程序授权状态,则会给出位置。我还可以检查用户是否拒绝了位置服务的权限 问题是当用户取消应用程序使用位置服务的授权,然后再次授权。在本例中,在此之后,如果我尝试获取位置,它会给出nil,尽管它调用了 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 状态为3的

我想获取设备的当前位置。代码正常工作。如果用户未更改位置服务的应用程序授权状态,则会给出位置。我还可以检查用户是否拒绝了位置服务的权限

问题是当用户取消应用程序使用位置服务的授权,然后再次授权。在本例中,在此之后,如果我尝试获取位置,它会给出
nil
,尽管它调用了

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
状态为
3
的委托方法,即
kCLAuthorizationStatusAuthorized

获取当前位置的代码:

CLLocation * location = self.locationManager.location;
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    DLog(@"Location authorization changed : %d", status);

    // If user has denied permission for location service
    if (status == kCLAuthorizationStatusDenied)
    {
        DLog(@"Location service denied.");

        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusNotDetermined)
    {
        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusAuthorized)
    {

    }
}
Getter方法:

- (CLLocationManager *)locationManager
{
    if (!locationManager)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
    }

    return locationManager;
}
CLLocationManager委托方法:

CLLocation * location = self.locationManager.location;
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    DLog(@"Location authorization changed : %d", status);

    // If user has denied permission for location service
    if (status == kCLAuthorizationStatusDenied)
    {
        DLog(@"Location service denied.");

        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusNotDetermined)
    {
        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusAuthorized)
    {

    }
}

你知道吗?

self.locationManager.location
nil
,因为你从未开始更新位置

apple文档中说明了locationManager的
位置
属性:

如果从未使用过位置数据,则此属性的值为零 找回了

因此,您需要以某种方式更新您的iPhone位置

通常这意味着你想打电话

[self.locationManager startUpdatingLocation]

但你也可以使用


[self.locationManager startMonitoringSignificantLocationChanges]

如果将委托设置为零,则不会获得有关授权状态更改的更新,不是吗

self.locationManager.delegate = nil;
我认为您应该保留代理以接收授权状态更新,然后调用startUpdatingLocation方法以获取当前位置

- (void)startUpdatingLocation

在上面的示例中,如果授权被撤销,则将locationManager.delegate设置为零。。。您是否再次将其设置为适当的类?刚才注意到您还将locationManager设置为零…根据您在某处重新创建
locationManager
对象的代码中的注释-您确定在设置
self.locationServiceDisabled=false
时会触发此操作?我看不到您开始更新您的位置(cllocationmanager startupdatinglocation)在apple文档中是locationManager的location属性:“如果从未检索到位置数据,则此属性的值为零。”因此,您需要以某种方式更新iPhone位置!为了实现这一点,您应该致电locationmanager startupdatinglocation。。。方法!如果您想仔细查看我的代码,那么很明显,在getter方法中,我再次设置了delegate。这将使这个答案更好。谢谢