iOS:应用程序在安装应用程序时未征求用户许可.每次都获取 kCLAuthorizationStatusNotDetermined - Objective-c &迅速 [英] iOS: App is not asking user's permission while installing the app. getting kCLAuthorizationStatusNotDetermined every time - Objective-c & Swift

查看:23
本文介绍了iOS:应用程序在安装应用程序时未征求用户许可.每次都获取 kCLAuthorizationStatusNotDetermined - Objective-c &迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 iOS 应用中获取用户位置.我首先在我的项目中包含了核心定位框架.然后单击按钮,我正在调用核心位置 api,如下所示.当我尝试在设备中安装它时,核心位置从不询问用户许可.当我尝试在按钮单击时获取位置时,我将 kCLAuthorizationStatusNotDetermined 作为授权状态.请帮助我.我不知道发生了什么.

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

这是我的 shouldFetchUserLocation 方法:

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

推荐答案

我遇到了同样的问题,重新安装我的应用程序后,每当检查 [CLLocationManager authorizationStatus] 并且该应用甚至没有出现在设置">隐私">定位服务"中.

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.

iOS 提示用户批准访问位置服务的授权对话框在 [locationManager startUpdatingLocation] 上触发,在您的情况下,该对话框从未被调用(shouldFetchUserLocation 将始终为 <代码>否).

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. 的解决方案似乎是一个不错的解决方案,将尝试这样做.

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

当 iOS8 到来时,它对 CLLocationManager 的使用方式几乎没有改变.正如在其他答案中多次提到的,与 iOS7 相比,它需要额外的步骤.今天我自己遇到了这个问题,发现了这篇文章(已经从多个其他问题中引用,但它完成了我之前的答案).希望对您有所帮助!

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天全站免登陆