我想获取设备的当前位置。代码正常工作正常。如果用户未更改位置服务的应用程序授权状态,则会给出位置。我也能够检查用户是否拒绝了位置服务的许可。获取当前位置的问题
问题是当用户取消授权应用程序使用位置服务,然后再次授权。在这种情况下,在此之后,如果我试图让位置它给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