iOS:应用程序在安装app.get kCLAuthorizationStatusNottDefined时没有征求用户的许可,每次都要使用Object-c&SWIFT。

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (94)

我试图在我的iOS应用程序中获取用户位置。我已经在我的项目中包括了重新安置的框架。然后,在一个按钮上单击,我将调用核心位置API,如下所示。当我试图在一个设备中安装它时,核心位置从来不需要用户的许可。当我尝试在按钮单击中获取位置时,我得到的是kCLAuthorizationStatusNotDefined为AuthisationStatus。请帮帮我。我不知道发生了什么。

- (IBAction)fetchLessAccurateLocation:(id)sender {
    [self.txtLocation setText:@""];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 1000;

    if ([self shouldFetchUserLocation]) {
        [locationManager startUpdatingLocation];
    }
}

这是我应该使用的FetchUserLocation方法:

-(BOOL)shouldFetchUserLocation{

    BOOL shouldFetchLocation= NO;

    if ([CLLocationManager locationServicesEnabled]) {
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusAuthorized:
                shouldFetchLocation= YES;
                break;
            case kCLAuthorizationStatusDenied:
            {
                UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"App level settings has been denied" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                [alert show];
                alert= nil;
            }
                break;
            case kCLAuthorizationStatusNotDetermined:
            {
                UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The user is yet to provide the permission" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                [alert show];
                alert= nil;
            }
                break;
            case kCLAuthorizationStatusRestricted:
            {
                UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The app is recstricted from using location services." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                [alert show];
                alert= nil;
            }
                break;

            default:
                break;
        }
    }
    else{
        UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        alert= nil;
    }

    return shouldFetchLocation;
}

以下是我的核心位置委托方法:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){

    NSLog(@"location fetched in delegate");

    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    if (abs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        NSLog(@"inside loop.... latitude %+.6f, longitude %+.6f\n",
              location.coordinate.latitude,
              location.coordinate.longitude);
    }
    NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    [self.txtLocation setText:[NSString stringWithFormat:@"\nlatitude: %+.6f \nlongitude:   %+.6f", location.coordinate.latitude, location.coordinate.longitude]];

    [locationManager stopUpdatingLocation];
    [locationManager stopMonitoringSignificantLocationChanges];

    if(locationManager!=nil){
        locationManager.delegate= nil;
        locationManager= nil;
    }
}
提问于
用户回答回答于

我在重新安装我的应用程序后面临同样的问题,它在kCLAuthorizationStatusNotDetermined每次检查时都会返回,[CLLocationManager authorizationStatus]并且应用程序甚至未在“设置”>“隐私”>“位置服务”中显示。

iOS会提示用户批准访问位置服务的授权对话框会触发[locationManager startUpdatingLocation],你的例子永远不会被调用(shouldFetchUserLocation始终NO)。

用户回答回答于

iOS8让我们对LocationsServices进行了主要的API更改

假设 [CLLocationManager locationServicesEnabled]返回YES,

首次推出iOS应用程序[iOS7和iOS8] - locationMangers(CLLocationManager)authorizationStatus预设为

authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined

在iOS7 +中提示

启动locationManger(CLLocationManager,Strong)并设置委托(CLLocationManagerDelegate)

现在要提示用户使用位置服务并在设置>隐私>位置服务中列出应用程序,它必须调用任何位置服务方法,其取决于应用程序要求 - 例如,如果应用程序具有以下某种

地点更新 - [self.locationManager startUpdatingLocation]

区域监控 - [self.locationManager startMonitoringForRegion:beaconRegion]

在执行上述方法之后,iOS会提示用户请求接受在应用程序中使用位置服务,并且不管用户选择的应用程序将在设置>隐私>位置服务下列出。

在iOS8 +中提示

它与iOS8的情况相同,首次启动应用程序位置服务

authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined

iOS 8我们提供了向用户显示提示的新方法

[self.locationManager requestAlwaysAuthorization] 
or
[self.locationManager requestWhenInUseAuthorization]

requestAlwaysAuthorization / requestWhenInUseAuthorization可用于iOS8。如果应用程序部署目标是iOS7,然后将其封装在if块中以确保在iOS7中不会导致应用程序崩溃。

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization]; .
}

要么

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}

很重要 ###:

根据iOS8,它必须包含说明为什么应用程序使用requestAlwaysAuthorization / requestWhenInUseAuthorization的字符串在info.plist中包含与应用程序要求相关的任何这些属性

对于kCLAuthorizationStatusAuthorizedAlways包含键/值(字符串值)对

NSLocationAlwaysUsageDescription = App use Locations service mode Always

对于kCLAuthorizationStatusAuthorizedWhenInUse包含键/值(字符串值)对

NSLocationWhenInUseUsageDescription = App use Locations service mode In Use 

info.plist的截图(如果有人被这个困惑)

扫码关注云+社区