我正在try 在iOS应用程序中获取用户位置.我首先在我的项目中加入了corelocation框架.然后点击一个按钮,我调用核心位置api,如下所示.当我试图将其安装到设备中时,核心位置从不请求用户许可.当我试图通过点击按钮获取位置时,我得到了kCLAuthorizationStatusNotDetermined作为授权状态.请帮帮我.我不知道发生了什么事.

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

推荐答案

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将始终是NO).

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

Edit for iOS8.x

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

Ios相关问答推荐

在Swift5中,将某些属性值从一个类复制到另一个类实例

与iPadOS中带有扣件的模式相似的组件是什么?

在SwiftUI中为圆角矩形创建均匀分布的点轮廓

如何解释在SwiftUI视图中观察到持续的数据更改

iOS应用程序冻结在UICollectionView代码,但我没有';没有任何UICollectionViews

为什么IOS多行文本输入在React原生纸张对话框中无法正常工作?

如何在Combine发布者之间强制执行最小延迟

如何更改 SwiftUI 弹出视图的大小?

uikit中如何触发swiftui功能

SwiftUI 菜单中的内联水平按钮

CGPath intersection(_:using:) 和朋友(iOS 16 中的新功能)坏了?

iOS应用程序崩溃,必需条件为false:isformatsamplerateandchannelcountvalid(format)

UIControl 子类 - 按下时显示 UIMenu

如何从 Locale Swift 获取货币符号

如何在字段包含字典数组的firestore上查询?

使用 Vim 代替(或与)Xcode 进行 iOS 开发

aps-environment 始终在发展

Xcode / iOS模拟器:手动触发重大位置更改

我可以通过 UIAppearance 代理设置哪些属性?

UITextView 内容插图