我想獲取設備的當前位置。代碼正常工作正常。如果用戶未更改位置服務的應用程序授權狀態,則會給出位置。我也能夠檢查用戶是否拒絕了位置服務的許可。獲取當前位置的問題
問題是當用戶取消授權應用程序使用位置服務,然後再次授權。在這種情況下,在此之後,如果我試圖讓位置它給nil
雖然它叫
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
委託方法與狀態3
即kCLAuthorizationStatusAuthorized
。
代碼獲取當前位置:
CLLocation * location = self.locationManager.location;
getter方法:
- (CLLocationManager *)locationManager
{
if (!locationManager)
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
}
return locationManager;
}
CLLocationManager委託方法:
- (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)
{
}
}
對此有何想法?
在上面你將locationManager.delegate設置爲nil,如果授權被撤銷......你是否曾經將它重新設置爲適當的類? – Volker
只是注意到你還將locationManager設置爲零... – Volker
根據您在代碼中的意見,您可以在某處重新創建'locationManager'對象 - 您確定在設置'self.locationServiceDisabled = false'時觸發了這個對象嗎? – Paulw11