简体   繁体   English

如何在iOS 4.2之前检查是否为特定应用启用了位置服务?

[英]How to check if location services are enabled for a particular app prior to iOS 4.2?

How can I check if the user has allowed location for mu app? 如何检查用户是否允许mu app的位置? Normally I would use authorizationStatus method of the CLLocationManager class, but it is only available in iOS 4.2 and newer. 通常我会使用CLLocationManager类的authorizationStatus方法,但它仅适用于iOS 4.2及更高版本。 Is it possible to achieve this somehow while still using SDK 4.2, so that the app can still run on devices with older versions of iOS, or do I have to downgrade the SDK? 是否有可能在使用SDK 4.2时以某种方式实现此目的,以便应用程序仍然可以在具有旧版iOS的设备上运行,或者我是否必须降级SDK? And along the same line, I need a similar alternative for the locationServicesEnabled method prior to iOS 4.0. 沿着同一条线,我需要一个类似于iOS 4.0之前的locationServicesEnabled方法的替代方案。

When you call -startUpdatingLocation , if location services were denied by the user, the location manager delegate will receive a call to -locationManager:didFailWithError: with the kCLErrorDenied error code. 当您调用-startUpdatingLocation ,如果用户拒绝了位置服务,则位置管理器委托将接收对-locationManager:didFailWithError:的调用,其中包含kCLErrorDenied错误代码。 This works both in all versions of iOS. 这适用于所有iOS版本。

I've combined two techniques in the code below 我在下面的代码中结合了两种技术

    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];
    }

Hmm.. I didn't think to use authorizationStatus or locationServicesEnabled . 嗯..我不认为使用authorizationStatuslocationServicesEnabled What I did was the following: 我做的是以下内容:

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;
}

I'm glad to see there's a better method to check, but I haven't had any problems with the way I detect if my app is allowed to use the GPS. 我很高兴看到有更好的检查方法,但是如果我的应用程序被允许使用GPS,我的检测方式也没有任何问题。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM