簡體   English   中英

如何在iOS 4.2之前檢查是否為特定應用啟用了位置服務?

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

如何檢查用戶是否允許mu app的位置? 通常我會使用CLLocationManager類的authorizationStatus方法,但它僅適用於iOS 4.2及更高版本。 是否有可能在使用SDK 4.2時以某種方式實現此目的,以便應用程序仍然可以在具有舊版iOS的設備上運行,或者我是否必須降級SDK? 沿着同一條線,我需要一個類似於iOS 4.0之前的locationServicesEnabled方法的替代方案。

當您調用-startUpdatingLocation ,如果用戶拒絕了位置服務,則位置管理器委托將接收對-locationManager:didFailWithError:的調用,其中包含kCLErrorDenied錯誤代碼。 這適用於所有iOS版本。

我在下面的代碼中結合了兩種技術

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

嗯..我不認為使用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,我的檢測方式也沒有任何問題。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM