Do not get permission from a user in MapKit

First you need to call -[CLLocationManager requestWhenInUseAuthorization]or -[CLLocationManager requestAlwaysAuthorization]. Actually, I called this method in Didload and even added in info.plist, still getting this error.

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"MapView";

    [MapView setMapType:MKMapTypeStandard];
    [MapView setZoomEnabled:YES];
    [MapView setScrollEnabled:YES];
    [MapView setShowsUserLocation:YES];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 23.0804 ;
    region.center.longitude = 72.5241;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [MapView setRegion:region animated:YES];

    [MapView setDelegate:self];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager requestAlwaysAuthorization];
    [locationManager requestWhenInUseAuthorization];

    [locationManager startUpdatingLocation];

    Display *ann = [[Display alloc] init];
    ann.title = @" Gujarat";
    ann.subtitle = @"High Court";
    ann.coordinate = region.center;
    [MapView addAnnotation:ann];

}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;
    if(annotation != MapView.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[MapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
        pinView.draggable = YES;
    }
    else {
        [MapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}


-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        [locationManager startUpdatingLocation];
    }
    else if (status == kCLAuthorizationStatusDenied)
    {
        //Alert to show for user if any when status is declined
    }
    else
        NSLog(@"Wrong location status");
}

and in the Display class for annotation

<MKAnnotation> {

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;

the code here does not display an invitation to obtain permission from the user to access his location, and because of this, we do not get the location of the desired place. Please help me be on it from yesterday.

enter image description here

+4
source share
4 answers

enter image description here

enter image description here

There were actually two info.plist files, and I changed the values ​​in the Plest Demo Test plist files. when I added the key NSLocationAlwaysUsageDescriptionor NSLocationWhenInUseUsageDescriptionin Info.plist It Worked ..

0

, .

1) CLLocation Manager.  , CLLocation, CLLocationManager , .

2) "" > "" > " ". , . , , , .

3) , requestAlwaysAuthorization:

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

4) .

-(void)checkStatus{

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

if (status==kCLAuthorizationStatusNotDetermined) {
_status.text = @"Not Determined";
}

if (status==kCLAuthorizationStatusDenied) {
_status.text = @"Denied";
}

if (status==kCLAuthorizationStatusRestricted) {
_status.text = @"Restricted";
}

if (status==kCLAuthorizationStatusAuthorizedAlways) {
_status.text = @"Always Allowed";
}

if (status==kCLAuthorizationStatusAuthorizedWhenInUse) {
_status.text = @"When In Use Allowed";
  }

}

1) http://www.devfright.com/location-authorization-ios-8/
2) http://nshipster.com/core-location-in-ios-8/

+2

:

  • CLLocationManager *locationManager . . , , , , .

  • [locationManager startUpdatingLocation] , .

    import

    import

    @interface ViewController () <CLLocationManagerDelegate>
    
    {
       CLLocationManager *locationManager;
    }
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad]; 
    
        [MapView setMapType:MKMapTypeStandard];
        [MapView setZoomEnabled:YES];
        [MapView setScrollEnabled:YES];
        [MapView setShowsUserLocation:YES];
        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = 23.0804 ;
        region.center.longitude = 72.5241;
        region.span.longitudeDelta = 0.01f;
        region.span.latitudeDelta = 0.01f;
        [MapView setRegion:region animated:YES];
    
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        {
            [locationManager requestWhenInUseAuthorization];
        }
    
        Display *ann = [[Display alloc] init];
        ann.title = @" Gujarat";
        ann.subtitle = @"High Court";
        ann.coordinate = region.center;
        [MapView addAnnotation:ann];
    }
    
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
            if (status == kCLAuthorizationStatusAuthorizedWhenInUse)
            {
                [locationManager startUpdatingLocation];
            }
            else if (status == kCLAuthorizationStatusDenied)
            {
            //Alert to show for user if any when status is declined
            }
            else
                NSLog(@"Wrong location status");
    }
    
+1

.plist:

NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist 

. . .

enter image description here

0

All Articles