简体   繁体   English

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

[英]CLLocationManager never asks user about location services

I have an IOS 8-only app in which I want to use location services to obtain the device's latitude and longitude. 我有一个仅限IOS 8的应用程序,我想在其中使用定位服务来获取设备的纬度和经度。 I think I have everything implemented correctly, but the app never asks the user if it's okay to use location services, the CLAuthorizationStatus never changes from kCLAuthorizationStatusNotDetermined and the CLLocationManager delegate method 我认为我已正确实施了所有操作,但应用程序从未询问用户是否可以使用位置服务,CLAuthorizationStatus从未从kCLAuthorizationStatusNotDetermined和CLLocationManager委托方法更改

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

never gets called. 永远不会被打电话。

Here's the CLLocationManager defined in the interface file for the viewcontroller: 这是在viewcontroller的接口文件中定义的CLLocationManager:

__strong CLLocationManager *locationManager;

Here's the code in the viewcontroller: 这是视图控制器中的代码:

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

The Info.plist file for the app has an entry for NSLocationWhenInUseUsageDescription. 应用程序的Info.plist文件具有NSLocationWhenInUseUsageDescription的条目。

Debug tracing shows that the line 调试跟踪显示该行

[locationManager requestWhenInUseAuthorization];

gets executed, but the dialog requesting the user to okay location services does not appear. 被执行,但是没有出现要求用户进行位置服务的对话框。

Location services in this app worked correctly under iOS 7 - I'm clearly doing something wrong, or not doing something I need to do to make it work in iOS 8. But I have dug around looking for insights, and it looks to me as if I'm doing everything correctly. 此应用中的位置服务在iOS 7下可以正常工作-我显然做错了什么,或者没有做我需要做的事情才能使其在iOS 8中正常工作。但是我一直在寻找洞察力,在我看来,如果我做的一切正确。

Any ideas and/or suggestions? 有什么想法和/或建议吗? Thanks in advance. 提前致谢。

You're pretty close. 你很亲密 Let me see if I can help. 让我看看是否可以帮忙。 For iOS 8, you need to first call: 对于iOS 8,您需要先调用:

[self.locationManager requestWhenInUseAuthorization];

I recommend putting this right after your line of code that says: locationManager.delegate = self; 我建议将此代码放在代码行之后,即: locationManager.delegate = self; (and then delete everything else in your viewDidLoad method). (然后删除viewDidLoad方法中的所有其他内容)。 This is different from iOS 7 due to all the new privacy stuff. 由于所有新的隐私设置,这与iOS 7不同。 Don't call startUpdatingLocation until after this method's delegate callback is called. 在调用此方法的委托回调之后,才调用startUpdatingLocation If you read the docs, you'll see that the method requestWhenInUseAuthorization , after finishing, calls its callback method: 如果您阅读了这些文档, requestWhenInUseAuthorization看到方法requestWhenInUseAuthorization在完成后调用其回调方法:

locationManager:didChangeAuthorizationStatus:

Within the callback method, check the status and take action depending on what that value is. 在回调方法中,检查状态并根据该值采取措施。 This is what my method looks like: 这就是我的方法:

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

Keep in mind that you won't see the alert asking if the app can use your location every time. 请记住,您不会看到提示询问应用程序是否每次都能使用您的位置。 After you've seen it once, it won't show again. 观看一次后,它将不再显示。 The user can then control whether or not the app can access his / her location in the settings app on the device. 然后,用户可以控制该应用程序是否可以访问其在设备上的设置应用程序中的位置。

first thing first, check for CLLocationManagerDelegate define or not then, do something like below. 首先,检查CLLocationManagerDelegate是否定义,然后执行以下操作。

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

    if ([systenversion integerValue]>=8.0) {

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

and the delegates, 和代表们,

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

hope this works. 希望这行得通。 :) :)

I found the solution, though I don't know why. 我找到了解决方案,尽管我不知道为什么。 I realized I had duplicate info.plists in different folders of the app's folder. 我意识到我在应用程序文件夹的不同文件夹中有重复的info.plists。 The two info.plists look to be identical, both with the NSLocationWhenInUseUsageDescription entry. 两个info.plists看起来都相同,都带有NSLocationWhenInInUseUsageDescription条目。 But when I removed the one not referred to in the app's navigation window, the app started to work as advertised: got the request alert, allowed location services and the heading started to update. 但是,当我删除了应用程序的导航窗口中未提及的应用程序时,该应用程序开始按广告宣传运行:收到了请求警报,允许的位置服务,并且标题开始更新。 No idea why an identical duplicate info.plist, not included in the app's build, would cause that problem, but it seems to be the case. 不知道为什么相同的重复info.plist(未包含在应用程序的构建中)会导致该问题,但事实确实如此。

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

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