简体   繁体   中英

Location Services permission don't get saved iOS

I have problem with the localization API in Core Location Services. I get the promt that request permission to the location services. If I click Allow and goes to Settings I can see that my app don't have permission.

This is my code in my GeoUtility class:

CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];

CLLocationManager *lm = [[CLLocationManager alloc] init];

[lm setPurpose:@"Need to verify your region"];
[lm startUpdatingLocation];

It's trigged by a viewController in the viewDidAppear

I also have added location-services under Required Devices Capabilites in my plist file.

Add CoreLocation.framework and MapKit.framework

And

in .h

#import <MapKit/MapKit.h>

CLLocationManager *locationManager;
CLGeocoder *geocoder;

in view did load

- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
        geocoder = [[CLGeocoder alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
}

then

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
        LongitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
        LatitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
    }


[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            NSString *addresstemp2 = [[NSString alloc] init];
            NSString *subThoroughfare2 = [NSString stringWithFormat:@"%@",placemark.subThoroughfare];

            NSString *thoroughfare2 = [NSString stringWithFormat:@"%@",placemark.thoroughfare];
            NSString *postalCode2 = [NSString stringWithFormat:@"%@",placemark.postalCode];
            NSString *locality2 = [NSString stringWithFormat:@"%@",placemark.locality];
            NSString *administrativeArea2 = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
            NSString *country2 = [NSString stringWithFormat:@"%@",placemark.country];

            if (![subThoroughfare2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@",subThoroughfare2];
            }
            if (![thoroughfare2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,thoroughfare2];
            }
            if (![postalCode2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,postalCode2];
            }
            if (![locality2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,locality2];
            }
            if (![administrativeArea2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,administrativeArea2];
            } 
            if (![country2 isEqualToString:@"(null)"]) {
                addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,country2];
            }
            AddressLbl.text = [[NSString alloc] initWithString:addresstemp2];
            [AddressLbl sizeToFit];

            [locationManager stopUpdatingLocation];
} else {
        }
    } ];
}

And you also change setting in your Simulator GO TO THE setting and choose Location Service change it to ON. see down is your application name if it is off then change to ON

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