iOS:应用程序在安装应用程序时不会询问用户的权限。每次获得kCLAuthorizationStatusNotDetermined - Objective-c&迅速 [英] iOS: App is not asking user's permission while installing the app. getting kCLAuthorizationStatusNotDetermined every time - Objective-c & Swift

查看:354
本文介绍了iOS:应用程序在安装应用程序时不会询问用户的权限。每次获得kCLAuthorizationStatusNotDetermined - Objective-c&迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在iOS应用中获取用户位置。首先在我的项目中加入了corelocation框架。然后在一个按钮上单击我调用核心位置API如下。当我试图将其安装在设备中时,核心位置从不要求用户许可。当我尝试获取按钮单击位置时,我将kCLAuthorizationStatusNotDetermined确定为authorisationStatus。请帮助我。我不知道发生了什么。

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


$ / code $ / pre
$ b $ p这是我的shouldFetchUserLocation方法:

   - (BOOL)shouldFetchUserLocation {

BOOL shouldFetchLocation = NO; ([CLLocationManager locationServicesEnabled]){
switch([CLLocationManager authorizationStatus]){
case kCLAuthorizationStatusAuthorized:
shouldFetchLocation = YES;

if
休息;
case kCLAuthorizationStatusDenied:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@错误消息:@应用程序级设置已被拒绝委托:无cancelButtonTitle:@确定otherButtonTitles:nil];
[alert show];
alert = nil;
}
break;
case kCLAuthorizationStatusNotDetermined:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@错误消息:@用户尚未提供权限delegate:nil cancelButtonTitle:@ OkotherButtonTitles:nil];
[alert show];
alert = nil;
}
break;
case kCLAuthorizationStatusRestricted:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@错误消息:@该应用程序使用位置服务进行了限制。委托人:无cancelButtonTitle:@OkotherButtonTitles:nil];
[alert show];
alert = nil;
}
break;

默认值:
break;

$ b $ else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@错误消息:@位置服务似乎从设置中被禁用。 委托人:无cancelButtonTitle:@OkotherButtonTitles:nil];
[alert show];
alert = nil;
}

返回shouldFetchLocation;
}

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

   - (void)locationManager:(CLLocationManager *)manager 
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__ MAC_NA,__ IPHONE_6_0){

NSLog(@在代理中获取的位置);

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

if(abs(howRecent)< 15.0){
//如果事件是最近发生的事情,请用它做些事情。
NSLog(@inside loop .... latitude%+。6f,longitude%+。6f \\\

location.coordinate.latitude,
location.coordinate.longitude) ;
}
NSLog(@latitude%+。6f,longitude%+。6f\\\

location.coordinate.latitude,
location.coordinate.longitude);
[self.txtLocation setText:[NSString stringWithFormat:@\\\
latitude:%+。6f \\\
longitude:%+。6f,location.coordinate.latitude,location.coordinate.longitude]];

[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];

if(locationManager!= nil){
locationManager.delegate = nil;
locationManager = nil;
}
}


解决方案

I面临同样的问题,重新安装我的应用程序后,它返回 kCLAuthorizationStatusNotDetermined ,每当检查 [CLLocationManager authorizationStatus] 和该应用程序甚至没有出现在设置>隐私>位置服务中。



iOS提示用户批准访问位置服务的授权对话在 [locationManager startUpdatingLocation] 在你的情况下永远不会被调用( shouldFetchUserLocation 将永远是 NO

Miguel C.的解决方案似乎是一个很好的解决方法,将会尝试。



为iOS8.x编辑



当iOS8出现时,它对CLLocationManager的使用方式几乎没有任何改变。正如其他答案中提到的那样,与iOS7相比,它需要额外的步骤。今天我自己面对这个问题,并发现这个文章(它已经从其他多个问题引用,但它完成了我以前的答案)。希望它有帮助!


I am trying to fetch user location in my iOS app. I have included corelocation framework in my project first. Then on a button click I am invoking the core location api as below. When I am trying to install this in a device, the core location never asks the user permission. When I try to fetch the location on button click, I am getting kCLAuthorizationStatusNotDetermined as the authorisationStatus. Please help me in this. I have no clue what is happening.

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

This is my shouldFetchUserLocation method:

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

Here is my core location delegate method:

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

解决方案

I was facing the same issue, after re-installing my app it was returning kCLAuthorizationStatusNotDetermined whenever checking for [CLLocationManager authorizationStatus] and the app didn't even show up in Settings > Privacy > Location Services.

The authorization dialog that iOS prompts user to approve access to location services is triggered on [locationManager startUpdatingLocation] which in your case is never called (shouldFetchUserLocation will be always NO).

Miguel C.'s solution seems like a good fix, will try that.

Edit for iOS8.x

When iOS8 came it brought little change in the way CLLocationManager is used. As mentioned few times in other answers it requires additional step comparing to iOS7. Today I faced the issue myself and found this article (it's been referenced from multiple other questions but it completes my earlier answer). Hope it helps!

这篇关于iOS:应用程序在安装应用程序时不会询问用户的权限。每次获得kCLAuthorizationStatusNotDetermined - Objective-c&amp;迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆