【问题标题】:How to check if location services are enabled for a particular app prior to iOS 4.2?如何检查是否为 iOS 4.2 之前的特定应用启用了位置服务?
【发布时间】:2011-06-09 17:10:42
【问题描述】:

如何检查用户是否允许 mu 应用程序的位置? 通常我会使用CLLocationManager 类的authorizationStatus 方法,但它只在iOS 4.2 和更新版本中可用。是否有可能在仍然使用 SDK 4.2 的同时以某种方式实现这一点,以便该应用程序仍然可以在具有旧版本 iOS 的设备上运行,还是我必须降级 SDK? 同样,对于 iOS 4.0 之前的 locationServicesEnabled 方法,我需要一个类似的替代方案。

【问题讨论】:

    标签: iphone cllocationmanager ios-4.2


    【解决方案1】:

    当您调用-startUpdatingLocation 时,如果用户拒绝了位置服务,位置管理器委托将收到对-locationManager:didFailWithError: 的调用,并带有kCLErrorDenied 错误代码。这适用于所有版本的 iOS。

    【讨论】:

    • 我喜欢打电话给[CLLocationManager locationServicesEnabled],如下所述。这样您就可以避免不必要的通话。
    • 对于在没有“以前的 iOS 4.2”约束的情况下偶然发现这里的用户,答案是:[CLLocationManager authorizationStatus]
    • @SpacyRicochet 是否回答了这个问题? OP 询问如何确定是否为特定应用启用了定位服务,[CLLocationManager locationServicesEnabled] 只是测试设备范围的定位服务。 [CLLocationManager authorizationStatus] 测试应用特定的授权。
    • @SpacyRicochet, [CLLocationManager locationServicesEnabled] 只检查通用的“定位服务”开关,不检查单个应用的状态。
    • @nous 现在是kCLAuthorizationStatusDenied
    【解决方案2】:

    我在下面的代码中结合了两种技术

        MKUserLocation *userLocation = map.userLocation;
        BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
        BOOL locationAvailable = userLocation.location!=nil;
    
        if (locationAllowed==NO) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                            message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else {
            if (locationAvailable==NO) 
                [self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
        }
    

    【讨论】:

    • 与上述相同的问题。请不要假设 nil 用户位置对应于没有授权,因为这可能不正确。
    • 我认为这不太正确。 [CLLocationManager locationServicesEnabled] 告诉您位置对所有应用程序是打开还是关闭,[CLLocationManager authorizationStatus] 告诉您应用程序是否有权检查位置。所以 UIAlertView 应该说打开位置而不是授权应用程序。但它很有用,谢谢@EasyCoder
    【解决方案3】:

    嗯.. 我没想过要使用 authorizationStatuslocationServicesEnabled。我所做的如下:

    MKUserLocation *userLocation = mapView.userLocation;
    
    if (!userLocation.location) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    

    我很高兴看到有更好的检查方法,但我检测我的应用是否允许使用 GPS 的方式没有任何问题。

    希望这会有所帮助!

    【讨论】:

    • 请注意,即使启用了定位服务,用户位置也可能不可用。例如,用户可能不在所有传感器的覆盖范围内(例如未打开 Wi-Fi 的 iPod touch)。
    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 2016-04-24
    • 2011-11-05
    • 2021-03-07
    • 2016-08-15
    • 2013-05-19
    相关资源
    最近更新 更多