简体   繁体   中英

iOS locationmanager doesn't ask user permission even with NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription in Info.plist

After submitting my App I need to get user location with CoreLocation (#import ) but it NEVER ask me the permission alert.

ViewController.h

#import "RootViewController.h"
#import < UIKit/UIKit.h >
#import < CoreLocation/CoreLocation.h >

@interface AroundMeViewController : RootViewController <CLLocationManagerDelegate>

@property(nonatomic, strong) CLLocationManager *locationManager;

...

@end

__________

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

NSLog(@"LOG: %d", [CLLocationManager locationServicesEnabled]);

self.locationManager = [[CLLocationManager alloc] init];
// Set a delegate to receive location callbacks
self.locationManager.delegate = self;

// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

....
// Start the location manager
[self.locationManager startUpdatingLocation];

NSLog(@"LOG: %@", self.locationManager);

.....

}

I really tried in so many way to solve this issue (different devices, allow manually from privacy settings) but I don't know where is my mistake.

According to NSHipster , permission to use location services must be asked explicitly, and is given asynchronously, so we can't immediately start location updates like we used to. We need to implement the

    locationManager:didChangeAuthorizationStatus

delegate method and start location updates there. This method is called any time the auth status changes based on user input.

It seems this delegate method is also called when locationManager is set up, as mine was triggering while splash screen was still showing (when I set locationManager delegate, I suspect), and then again when I called triggerLocationServices(). This could be what is in effect "silencing" your request.

I changed my methods to the following:

    func triggerLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            if self.locationManager.respondsToSelector("requestAlwaysAuthorization") {
                // request authorization to use location
            } else {
                // start updating location
            }
        }
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){
        if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
            // start updating location
        } 
        else {
            triggerLocationServices()
        }
    }

and removed any other calls to triggerLocationServices(). This, along with the correct info.plist keys, resolved it for me.

Apologies for Swift code, hope you can translate into Obj-C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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