-[CLLocationManager requestWhenInUseAuthorization]

2020-06-09 07:43发布

This is my code, showing both the alert and the blue dot for the current position on the map:

MapName.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface MapName : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *MapName;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

MapName.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

//Center the map
[self gotoLocation];

//Show current position
_MapName.showsUserLocation = YES;

}

I've added the key NSLocationWhenIsUseUsageDescription as a string to the Info.plist. I'm still getting the same error on Xcode.

6条回答
可以哭但决不认输i
2楼-- · 2020-06-09 08:27

It is due to both:

[self.locationManager startUpdatingLocation];

and

_MapName.showsUserLocation = YES;

You need to check if the user has given permission before you call these. Also make sure you turn off User Location in the MKMapKit on the storyboard (this one took me days to track down).

Do something like:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    [self.locationManager startUpdatingLocation];       
    _MapName.showsUserLocation = YES;        

}

Depending on your app you may not want to ask for the user's permission on launch since that is not recommended.

查看更多
Emotional °昔
3楼-- · 2020-06-09 08:29

This way is working without errors on Xcode6:

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) UIWindow *window;

@end 

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize locationManager = _locationManager;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions
 {
//Current position
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];

return YES;
}
@end

The alert message shows when you open the app for the first time

查看更多
虎瘦雄心在
4楼-- · 2020-06-09 08:33

On swift :

  let locationManager: CLLocationManager = CLLocationManager()
  let authorizationStatus = CLLocationManager.authorizationStatus()


  override func viewDidLoad() {
    super.viewDidLoad()
    if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
       locationManager.startUpdatingLocation()
    }
    else
    {
      locationManager.requestWhenInUseAuthorization()
    }
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

}

Don't forget to add into Info.plist file these keys:

enter image description here

查看更多
贪生不怕死
5楼-- · 2020-06-09 08:36

The keyword in the info file is ("In" instead of "Is"):

NSLocationWhenInUseUsageDescription
查看更多
爷的心禁止访问
6楼-- · 2020-06-09 08:37

The error message is pretty literal. Don't call [self.locationManager startUpdatingLocation] until you have authorization. Your [self.locationManager requestWhenInUseAuthorization], per the docs, is asynchronous.

When the current authorization status is kCLAuthorizationStatusNotDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services.

This means you are both prompting for access as well as starting your scan at the same time.

Instead, try implementing -[CLLocationManagerDelegate locationManager:didChangeAuthorizationStatus:] and starting your scan there after it's been determined that you have authorization.

查看更多
迷人小祖宗
7楼-- · 2020-06-09 08:49

Based off of Jerome's response, I translated the answer that worked for me to Swift.

    let authorizationStatus = CLLocationManager.authorizationStatus()

    if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
           self.findMyLocation()
    }

Note: From what I read in the documents, there is no kCLAuthorizationStatusAuthorized equivalent in Swift.

查看更多
登录 后发表回答