如果启用了位置服务,我测试了设备(iPod Touch 2G iOS 4.1)

permitted = [locationManager locationServicesEnabled];

无论是否启用位置服务,我总是会得到一个是。我说的是位置服务的常规按钮,而不是应用程序特定按钮。在带有iOS 3.2.2的iPad上,一切正常。

有帮助吗?

解决方案

当您实施位置管理器的代表时,您应该实现didfailwitherror。在那里,如果用户不允许访问位置,您将获得适当的错误

Apple文档指出:如果用户拒绝您的应用程序对位置服务的使用,则此方法报告 kCLErrorDenied 错误。收到此类错误后,您应该停止位置服务。

其他提示

记住这一点 [locationManager locationServicesEnabled]自iOS 4.0以来弃用。使用类方法 [CLLocationManager locationServicesEnabled] 反而。

应用程序特定按钮可以通过

[CLLocationManager authorizationStatus]

当您使用时

[CLLocationManager locationServicesEnabled]

然后,您检查整个系统中是否启用了位置服务。因此,当您进入设置 - >位置服务时,您会看到第一个开关。该方法返回该状态的状态,并且与您的应用程序无关。

如果您需要知道您的应用程序是否可以访问位置服务,请使用@Pascalius答案。

迅速 3.1功能返回 - >状态:布尔和消息:字符串

func isLocationEnabled() -> (status: Bool, message: String) {
    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
            return (false,"No access")
        case .authorizedAlways, .authorizedWhenInUse:
            return(true,"Access")
        }
    } else {
        return(false,"Turn On Location Services to Allow App to Determine Your Location")
    }
}
if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways))
{
        ; // app doesn't have access to localization to whatever you want
}

CllocationManager locationservicesEnabled]当用户设置按钮切换到OFF时,将返回否,只有这样我才达到NO。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top