繁体   English   中英

CLLocationManager从不询问用户有关位置服务的信息

[英]CLLocationManager never asks user about location services

我有一个仅限IOS 8的应用程序,我想在其中使用定位服务来获取设备的纬度和经度。 我认为我已正确实施了所有操作,但应用程序从未询问用户是否可以使用位置服务,CLAuthorizationStatus从未从kCLAuthorizationStatusNotDetermined和CLLocationManager委托方法更改

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 

永远不会被打电话。

这是在viewcontroller的接口文件中定义的CLLocationManager:

__strong CLLocationManager *locationManager;

这是视图控制器中的代码:

- (void)viewDidLoad{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
    //... other viewDidLoad code
}

应用程序的Info.plist文件具有NSLocationWhenInUseUsageDescription的条目。

调试跟踪显示该行

[locationManager requestWhenInUseAuthorization];

被执行,但是没有出现要求用户进行位置服务的对话框。

此应用中的位置服务在iOS 7下可以正常工作-我显然做错了什么,或者没有做我需要做的事情才能使其在iOS 8中正常工作。但是我一直在寻找洞察力,在我看来,如果我做的一切正确。

有什么想法和/或建议吗? 提前致谢。

你很亲密 让我看看是否可以帮忙。 对于iOS 8,您需要先调用:

[self.locationManager requestWhenInUseAuthorization];

我建议将此代码放在代码行之后,即: locationManager.delegate = self; (然后删除viewDidLoad方法中的所有其他内容)。 由于所有新的隐私设置,这与iOS 7不同。 在调用此方法的委托回调之后,才调用startUpdatingLocation 如果您阅读了这些文档, requestWhenInUseAuthorization看到方法requestWhenInUseAuthorization在完成后调用其回调方法:

locationManager:didChangeAuthorizationStatus:

在回调方法中,检查状态并根据该值采取措施。 这就是我的方法:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if(status == kCLAuthorizationStatusAuthorized || status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        [self.locationManager startUpdatingLocation];
    }
}

请记住,您不会看到提示询问应用程序是否每次都能使用您的位置。 观看一次后,它将不再显示。 然后,用户可以控制该应用程序是否可以访问其在设备上的设置应用程序中的位置。

首先,检查CLLocationManagerDelegate是否定义,然后执行以下操作。

NSString *systenversion=[[UIDevice currentDevice] systemVersion];

    if ([systenversion integerValue]>=8.0) {

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

和代表们,

- (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: {
            NSLog(@"User still thinking..");
        } break;
        case kCLAuthorizationStatusDenied: {
            NSLog(@"User hates you");
        } break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways: {
            [locationManager startUpdatingLocation];//Will update location immediately

            [self showAlertWithTitle:@"Success" andMessge:@"Success to get current location"];
        } break;
        default:
            break;
    }
}

希望这行得通。 :)

我找到了解决方案,尽管我不知道为什么。 我意识到我在应用程序文件夹的不同文件夹中有重复的info.plists。 两个info.plists看起来都相同,都带有NSLocationWhenInInUseUsageDescription条目。 但是,当我删除了应用程序的导航窗口中未提及的应用程序时,该应用程序开始按广告宣传运行:收到了请求警报,允许的位置服务,并且标题开始更新。 不知道为什么相同的重复info.plist(未包含在应用程序的构建中)会导致该问题,但事实确实如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM