iOS:应用程序未询问用户';在安装应用程序时,请检查其权限。每次都不确定KClauthorizationStatus-目标-c&;敏捷的

iOS:应用程序未询问用户';在安装应用程序时,请检查其权限。每次都不确定KClauthorizationStatus-目标-c&;敏捷的,ios,gps,core-location,cllocationmanager,Ios,Gps,Core Location,Cllocationmanager,我正在尝试获取iOS应用程序中的用户位置。我首先在我的项目中包括了corelocation框架。然后点击一个按钮,我调用核心位置api,如下所示。当我试图将其安装到设备中时,核心位置从不请求用户权限。当我尝试在单击按钮时获取位置时,我得到的KClauthorizationStatusNotDetected为AuthorizationStatus。请帮我做这件事。我不知道发生了什么事 - (IBAction)fetchLessAccurateLocation:(id)sender { [s

我正在尝试获取iOS应用程序中的用户位置。我首先在我的项目中包括了corelocation框架。然后点击一个按钮,我调用核心位置api,如下所示。当我试图将其安装到设备中时,核心位置从不请求用户权限。当我尝试在单击按钮时获取位置时,我得到的KClauthorizationStatusNotDetected为AuthorizationStatus。请帮我做这件事。我不知道发生了什么事

- (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方法:

-(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;
    }
}
-(无效)locationManager:(CLLocationManager*)manager
didUpdateLocations:(NSArray*)位置\uuuuuOSX\uU可用\uuuU启动(\uuuuuMac\uU NA,\uuuuuuuuuiPhone\u6\u0){
NSLog(@“在委托中获取的位置”);
CLLocation*location=[locations lastObject];
NSDate*eventDate=location.timestamp;
NSTimeInterval howRecent=[eventDate timeIntervalSinceNow];
如果(abs(最近几年)<15.0){
//如果这件事是最近发生的,那就做点什么吧。
NSLog(@“环内….纬度%+.6f,经度%+.6f\n”,
位置坐标纬度,
位置、坐标、经度);
}
NSLog(@“纬度%+.6f,经度%+.6f\n”,
位置坐标纬度,
位置、坐标、经度);
[self.txtLocation setText:[NSString stringWithFormat:@“\n纬度:%+.6f\n长度:%+.6f”,位置.坐标.纬度,位置.坐标.经度]];
[locationManager停止更新位置];
[locationManager停止监视重要的位置更改];
如果(locationManager!=无){
locationManager.delegate=nil;
locationManager=零;
}
}
尝试添加

[locationManager startUpdatingLocation]

还要确保位置服务已打开

编辑:


同时尝试删除应用程序并重新安装。可能存在应用程序正在读取的记录,这会阻止它请求使用位置的权限。

卸载应用程序,然后再次尝试运行它

如果不起作用,请转到“设置”并禁用对该应用的授权。然后再次运行它,查看它是否请求权限

或:

您可以使用以下代码强制应用程序开始监视该位置:

self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
在委托方法中,您可以检测获取位置是否有错误,并可以通知用户

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // Delegate of the location manager, when you have an error
    NSLog(@"didFailWithError: %@", error);

    UIAlertView *errorAlert = [[UIAlertView alloc]     initWithTitle:NSLocalizedString(@"application_name", nil) message:NSLocalizedString(@"location_error", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil];

    [errorAlert show];
}

如果您有任何问题,请告诉我。

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

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

Miguel C.的解决方案似乎是一个很好的解决方案,我会尝试一下

为iOS8.x编辑
当iOS8出现时,它对CLLocationManager的使用方式几乎没有改变。正如在其他答案中多次提到的,与iOS7相比,它需要额外的步骤。今天我自己面对这个问题,发现了这一点(从多个其他问题中都有提及,但它完成了我先前的回答)。希望有帮助

自从iOS 7发布以来,Rashmi Ranjan mallick在评论中多次提到这个问题。我真的认为这是iOS 7的一个bug,新版本的iOS中有很多bug

对我来说,问题是一样的:

1.打开应用程序,从不询问位置。iOS没有询问许可证的问题
2.我进入设置->隐私->位置服务,但我的应用程序不在那里
3.我无法以任何方式更改我的应用程序的位置权限

一个解决方法是:

1.杀死你的应用程序
2.使用主开关按钮禁用所有位置服务
3.再次转到你的应用程序
4.关闭你的应用程序
5.在上一个开关按钮上再次启用定位服务
6.如果仍然没有出现在列表中,请再次转到你的应用程序,再次关闭它,然后返回设置
7.现在它应该在那里了

这对我很有用,我希望这是有用的

更新:如果iOS 8请确保在未使用授权时调用
请求
请求始终授权

您可以按照以下代码进行操作:)


如果您在iOS8上遇到此问题,请使用:RequestWhenUseAuthorization。

您必须在您的
info.plist中添加
nsLocationWhenUseDescription
。这有点奇怪,但只需将其添加为不带文本的字符串值,然后在需要位置时从以下内容开始:

CLLocationManager * locationManager = [[CLLocationManager alloc] init];

[locationManager requestWhenInUseAuthorization];
iOS8对LocationsServices进行了重大的API更改 假定
[CLLocationManager LocationServiceEnabled]
返回是

随着iOS应用程序[iOS7和iOS8]的首次启动,LocationManager(CLLocationManager)authorizationStatus 预设为

authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined
iOS7中的提示+ 启动LocationManager(CLLocationManager ,Strong)并设置委托(CLLocationManagerDelegate)

现在,要提示用户使用位置服务并在“设置>隐私>位置服务”下列出应用程序,其必须调用任何位置服务方法,这取决于应用程序要求-例如,如果应用程序属于以下类型之一

位置更新-
[self.locationManager startUpdatingLocation]

地区监控-
[self.locationManager启动地区监控:beaconRegion]

在执行上述方法之后,iOS将立即提示用户请求接受Lo的使用
authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined
authorizationStatus(CLAuthorizationStatus) = kCLAuthorizationStatusNotDetermined
[self.locationManager requestAlwaysAuthorization] 
or
[self.locationManager requestWhenInUseAuthorization]
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization]; .
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
NSLocationAlwaysUsageDescription = App use Locations service mode Always
NSLocationWhenInUseUsageDescription = App use Locations service mode In Use 
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
-(void)initLocationManager{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;

    [locationManager requestAlwaysAuthorization]; // HERE ASK FOR THE RELEVANT
}
-(void) updateLocation {
   CLLocationManager *locationManager = ...
}
CLLocationManager *locationManager;

-(void) updateLocation {
  self.locationManager = ...
}
 1. NSLocationAlwaysAndWhenInUseUsageDescription 
 2. NSLocationWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
    [locationManager requestWhenInUseAuthorization];
}else{
    [locationManager startUpdatingLocation];
} 
#pragma mark - Lolcation Update 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied:
        {
            // do some error handling
        }
            break;
        default:{
            [locationManager startUpdatingLocation];
        }
            break;
    }
}
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    userLatitude =  [NSString stringWithFormat:@"%f", location.coordinate.latitude] ;
    userLongitude =  [NSString stringWithFormat:@"%f",location.coordinate.longitude];
    [locationManager stopUpdatingLocation];
}
 1. NSLocationAlwaysAndWhenInUseUsageDescription 
 2. NSLocationWhenInUseUsageDescription
import CoreLocation
class ViewController: UIViewController ,CLLocationManagerDelegate {
var locationManager = CLLocationManager()

//MARK- Update Location 
func updateMyLocation(){
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    if locationManager.respondsToSelector(#selector(CLLocationManager.requestWhenInUseAuthorization)){
       locationManager.requestWhenInUseAuthorization()
    }
    else{
        locationManager.startUpdatingLocation()
    }
}
//MARK: Location Update
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    NSLog("Error to update location :%@",error)
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    switch status {
    case .NotDetermined: break
    case .Restricted: break
    case .Denied:
            NSLog("do some error handling")
        break
    default:
        locationManager.startUpdatingLocation()
    }
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     let location = locations.last! as CLLocation
    var latitude = location.coordinate.latitude
    var longitude = location.coordinate.longitude

}