2

I cannot get even the simplest location updating code to work. I have a location app that worked fine for over a year, and now when I try to compile and run it (after upgrading to xcode 6.1 and with no changes to the code), after calling startUpdatingLocation, the didUpdateLocations callback never fires, and I can see that the gps indicator never appears next to the battery indicator as it should. I have started a new test project that does nothing but attempts to register for location updates, and still the same results: no location updates on device or simulator. Here is the code for the single view controller of the test project:

//ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{

}

@property (strong, nonatomic) CLLocationManager* locationManager;

@end


//ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"got a location");
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     NSLog(@"got a location");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

It would be fantastic if someone with the latest version of xcode could create a project like this that does nothing but receive location updates, verify that it works, and post the code in full. I have tried adding things to the plist file and all other remedies from similar questions to no avail.

steven
  • 347
  • 1
  • 2
  • 10
  • Worked fine for me when I added NSLocationWhenInUseUsageDescription to the info.plist. Your code was copy & pasted into a new single view application. – InsertWittyName Oct 26 '14 at 20:44

1 Answers1

1

Yes, a few things have changed in the new version and it can be a headache. I was having the same problem and this is the thread that solved it for me here.

You need to add these lines 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 make sure that you call [CLLocationManager requestWhenInUseAuthorization] or [CLLocationManager requestAlwaysAuthorization] before you try to update the users location.

Community
  • 1
  • 1
Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
  • I had tried adding all of this previously, but when I copied your plist lines directly into the plist source, it worked! Thank you for the magic combination! I was teetering on insanity. – steven Oct 26 '14 at 21:24