7

I just started my project for iOS 8 and i ran in too the problem that i cant get the question to pop up for permission. I added the following to my info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>

and this is my code :

@interface ViewController () <MKMapViewDelegate>
@property (nonatomic, strong) UIPopoverController* userDataPopover;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic, retain) CLLocationManager *locationManager;
@end

@implementation ViewController

-(CLLocationManager *)locationManager
{
    if(!_locationManager) _locationManager = [[CLLocationManager alloc]init];
    return _locationManager;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;
        [self.locationManager requestWhenInUseAuthorization];
        //[self.locationManager requestAlwaysAuthorization];

    [self.locationManager startUpdatingLocation];

    self.mapView.showsUserLocation = YES;
    [self.mapView showsUserLocation];
    [self.mapView setMapType:MKMapTypeStandard];
    [self.mapView setZoomEnabled:YES];
    [self.mapView setScrollEnabled:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
Sanne
  • 427
  • 6
  • 16
  • The whole location thing in iOS8 has broken my app. Was working fine in iOS7 and now with the permission changes everything has broken. Will be keeping an eye on this. – Fogmeister Sep 18 '14 at 10:38
  • 1
    I find the solution in my code. When i left out this line : [self.locationManager startUpdatingLocation]; everything started working! Hope that may be of any help to you! – Sanne Sep 18 '14 at 10:51
  • 2
    upvoted for the awesome text in your plist! – Paul Cezanne Sep 18 '14 at 13:26

2 Answers2

7

Make sure your view controller implements CLLocationManagerDelegate and imports #import CoreLocation/CoreLocation.h

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setLocationManager:[[CLLocationManager alloc] init]];

    if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [self.mapview setShowsUserLocation:NO];
        [self.locationManager setDelegate:self];
        [self.locationManager requestWhenInUseAuthorization];
    }
    else{
        //Before iOS 8
        [self.mapview setShowsUserLocation:YES];
    }
}

Add this method to listen for AuthorizationStatus changes

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
   if (status == kCLAuthorizationStatusDenied) {
       // permission denied
       [self.mapview setShowsUserLocation:NO];
   }
   else if (status == kCLAuthorizationStatusAuthorized) {
       // permission granted
       [self.mapview setShowsUserLocation:YES];
   }
}

Finally make sure your plist has the following entries as pointed out by Sanne

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
marco
  • 266
  • 2
  • 5
1

its new for ios 10 Now add location permission in plist file

if not add permission in plist then permission popup is not show

Add below permission in plist

1.Privacy - Location When In Use Usage Description

2.Privacy - Location Always Usage Description

yogesh
  • 138
  • 1
  • 8