【发布时间】:2014-04-01 10:05:05
【问题描述】:
我想获取设备的当前位置。代码正常工作。如果用户没有更改位置服务的应用授权状态,它会给出位置。我还可以检查用户是否拒绝了位置服务权限。
问题是当用户取消授权应用程序使用定位服务然后再次授权时。在这种情况下,在此之后,如果我尝试获取位置,它会给出 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 如果授权被撤销......你有没有再次将它设置为适当的类?
-
刚刚注意到您还将 locationManager 设置为 nil...
-
根据您在代码中的 cmets 在某处重新创建
locationManager对象 - 您确定在设置self.locationServiceDisabled = false时会触发此操作吗? -
我没有看到你开始更新你的位置 (cllocationmanager startupdatinglocation)
-
在苹果文档中,它是关于 locationManager 的位置属性的状态:“如果没有检索到任何位置数据,则此属性的值为 nil。”因此,您需要以某种方式更新您的 iPhone 位置!为了实现这一点,您应该调用 locationmanager startupdatinglocation... 方法!
标签: ios objective-c ios7 cllocationmanager cllocation