0

I have an issue showing user locations in objective C. I tried everything i could find here in stackoverflow, aaaand, didn't work.

So I have that code :

-(void)setLocation
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc]init];


    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    locationManager.distanceFilter = 10.0;
    [locationManager startUpdatingLocation];
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];



    myAnnotation.coordinate = mapView.userLocation.location.coordinate;

    myAnnotation.title = @"Test";
    myAnnotation.subtitle = @"I am a test Subtitle";

    [self.mapView addAnnotation:myAnnotation];
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    [self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
}

Everything is in ViewController.m, more precisely in a mapView declared in my .h file:

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

Does anyone have any idea ? The error i have is that:

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

Thanks :)

  • Did you forget to set the proper key-values in info.plist? http://stackoverflow.com/a/24064860/653513 – Rok Jarc May 31 '16 at 15:35
  • I'll do as you said. I saw something like that in other posts, but since i didn't understand, well... I hoped it wasn't important. I should never trust my intuition. Thanks anyway :) – Adèle Dunand Jun 01 '16 at 07:59
  • Edit :Ok, I just tried few choices proposed in the post you linked and nothing worked. I have a message display on my application lauching, « this application requires location services… » so it recognized I added the line. But still, the error messaged is here, and nothing changed. I tried one and the other sentence, I tried both, and 5 props of the posts. – Adèle Dunand Jun 01 '16 at 08:19
  • Hmm, try to declare locationManager as an instance variable or as a property. It shouldn't be declared as a local variable inside a method: which is in your case. – Rok Jarc Jun 01 '16 at 08:50

2 Answers2

0

Look at the following lines

[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];

An error says that you just started the location updates without prompting for location authorization.

Your starting the location update before prompting for the location authorization. So just order of these lines is wrong.

1) Prompt user for location update

2) And then start update location

So your code should be in following order.

//1
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];

//2
[locationManager startUpdatingLocation];

Don't forget to add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist

MD.
  • 1,157
  • 11
  • 18
  • I just modified my code as yours, and it's still not working. I'm going to explore the "info.plist" way, I have no clue of what it's about. Thanks anyway :D – Adèle Dunand Jun 01 '16 at 07:57
  • @AdèleDunand, it's Compulsory to add one of the above key in info.plist file from iOS 8 to access the user current location . [Look at this image](http://i.stack.imgur.com/iuOzB.jpg) and read [this answer](http://stackoverflow.com/questions/24062509/location-services-not-working-in-ios-8#answer-24063578). – MD. Jun 01 '16 at 08:14
  • it's exactly the post i read, and the line I added :) But the problem was more about... well the chair/keyboard interface – Adèle Dunand Jun 01 '16 at 09:08
0

I can't post this as a comment so i'll post it here.

You can simply open your Info.plist file in TextEdit and add these lines.

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
    <string>external-accessory</string>
    <string>remote-notification</string>
</array>
<key>NSLocationUsageDescription</key>
<string>App needs to use GPS to keep track of your activity</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App needs to use GPS to keep track of your activity</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
    <string>gps</string>
</array>

EDIT:

I see that your locationManager is a local method variable. It should declared be as an instance variable or as a property.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • Rofl. I didn't even need to edit my info.plist file (knowing that i already added the NSLocationsomething line in it). It was all about my locationManager that wasn't a local method variable. Thanks a lot. Like really, a lot. – Adèle Dunand Jun 01 '16 at 09:03