IOS 8 CLLocationManager Issue (Authorization Not Working)

落花浮王杯 提交于 2019-12-10 10:36:11

问题


#import "MyLocationViewController.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

@interface MyLocationViewController ()

@end

@implementation MyLocationViewController

{
    CLLocationManager *locationManager;
}

- (void)requestAlwaysAuthorization
{
    [locationManager requestAlwaysAuthorization];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    locationManager = [[CLLocationManager alloc] init];

}

- (IBAction)unwindToMap:(UIStoryboardSegue *)segue
{

}

- (IBAction)getCurrentLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion mapRegion;
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.001;
    mapRegion.span.longitudeDelta = 0.001;




    CLLocationCoordinate2D location = userLocation.coordinate;
    float lat = location.latitude;
    float lng = location.longitude;

    NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithFloat:lat] , @"Latitude",
                                        [NSNumber numberWithFloat:lng], @"Longitude", nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                locationDictionary, @"Location_A",
                                nil];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];

    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);



    if (mapView.userLocation != nil)
    {
        _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.longitude];
        _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.latitude];
    }

    [mapView setRegion:mapRegion animated: YES];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

This is my code.

Now, I understand that I needed to edit my info.plist file (which I did) with a NSLocationAlwaysUsageDescription key, and I added a string for the description.

However, I'm having issues implementing the authorization portion, as the error still reads as such:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

I've read the Apple IOS 8 docs for the CLLocationManager, but somehow my code will not work.

Can someone help me so that the above error message goes away by looking at where in my code I need to modify so that it works?

Thanks!


回答1:


You should not turn on MKMapView's showsUserLocation before the user has authorised your app to use Location services.

You can implement CLLocationManagerDelegate's locationManager:didChangeAuthorizationStatus: method and turn on showsUserLocation there like so:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {  
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        self.mapView.showsUserLocation = YES;
    }
}


来源:https://stackoverflow.com/questions/25222188/ios-8-cllocationmanager-issue-authorization-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!