57

Code below should get current location. But above error is generated. Function didUpdateLocations never gets called.

Running this on a device (iPhone 5s)iOS 9.1. Info.plist has Required device capabilities and Privacy - Location usage description configured as shown in the attached image. Please help!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.requestLocation()
}


func locationManager(manager: CLLocationManager,
    didFailWithError error: NSError) 
    print(error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations
      locations: [CLLocation]) {        
 print(“got location”)
}

Info.plist

Tusshu
  • 1,664
  • 3
  • 16
  • 32
  • Hey do u find any solutions for this – siva krishna Oct 27 '15 at 06:26
  • Hi @user1848653: Could you mark my my answer as accepted ? I quite often receive +1 and it would give it more visibility to help developers with this error. – H4Hugo Apr 15 '16 at 08:58
  • Please check it on real device too. I was also facing same issue but it is working on real device. – Chandni Nov 24 '17 at 05:04
  • In addition to the solution provided by H4Hugo, I also needed to delete the app in the simulator and run again in X11. – Merid Apr 16 '20 at 17:42

9 Answers9

90

Please try this :

Go to :

  • Product -> Scheme -> Edit Scheme -> Options -> Allow Location Simulation must be checked and try providing a default location, don't leave it set to "none"

EDIT : As mentionned in the comments, also restart Xcode and you're done !

Hope this helps.

H4Hugo
  • 2,570
  • 3
  • 16
  • 32
  • 1
    I can confirm this works. I had the exact same problem as mentioned in the original question and while "Allow Location Simulation" was checked I had the default location set to none. Changing this to a default location and then restarting Xcode solved the issue. – tmarkiewicz Feb 15 '16 at 17:27
  • @user212514 I faced this error again on an other project, used the method I provided to correct it and didn't have to restart Xcode. Can you confirm you were having the same error message displayed ? – H4Hugo May 07 '16 at 15:06
  • Sorry, I can't confirm it was the exact same message. I mostly think it was exactly the same, but it seems to be all gone and I don't know how to recreate it. – user212514 May 08 '16 at 05:12
  • Thank you this was my issue also – Cornelis Kuijpers Apr 09 '20 at 10:18
  • Thank you for the solution! Changed the default location from "none" to the 1st value and location services started working, even without restarting Xcode. – Nekto Aug 23 '20 at 14:45
32

In XCode 11.5 and up...

Open your Simulator:

Features => Location => CustomLocation

enter image description here

Roberto Rodriguez
  • 3,179
  • 32
  • 31
4

If the location authorization alert is not popping up for you, try adding NSLocationAlwaysUsageDescription to your plist as type string. Also, do the same for NSLocationWhenInUseUsageDescription.

4

searching google true the answers this seems to be a problem since IOS4.0.

in XCODE 7.1 under Project => scheme => edit scheme.

under Run => Options => Core location / disable Allow Location Simulation.

in your IOS Simulator under Debug => Location select a location. This fixed the problem for me.

DustPhyte
  • 41
  • 1
  • 9
4

Try this from the Simulator --> Debug --> Location

enter image description here

PrimeDime
  • 129
  • 1
  • 5
3

I was having the same issue with an app that I was moving from 8.0 -> 9.0/9.1. I tried a couple of things like write the CLLocationManager portion only in Swift and a new Obj-C project as well. I also tested it on different devices. To solve the issue on the device, I removed the didFailWithError func. Which stops the error altogether. This may not seem the best idea but in IOS9 you can limit your application to devices that have GPS or Location-Services. Android has been doing this for a really long time. I also noticed in your .plist your don't have the permissions enabled for NSLocationALwaysUsageDescription or the NSLocationWhenInUseUsageDescrtiption properties included.

Just for reference I have attached what my current .plist looks like that does not fire this error as well as a code chunk in obj-c the controller file. The code starts at the very end of a previous function and then has the commented out didFailWithError delegate and the didUpdateLocations. Currently this is working successfully in IOS 9.1

       //Get map authorization
    [CLLocationManager locationServicesEnabled];
    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.allowsBackgroundLocationUpdates = YES;
    if(nil == locationManager) locationManager = [[CLLocationManager alloc]init];
    if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [locationManager requestAlwaysAuthorization];
    }
    //locationManager.distanceFilter=10;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];



//- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
//{
//    NSLog(@"didFailWithError: %@", error);
//    UIAlertView *errorAlert = [[UIAlertView alloc]
//                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//    [errorAlert show];
//}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


    NSLog(@"LocationUpdated: %@",[locations lastObject]);
    [self updateMapMarkers];

//    NSDate* eventDate = location.timestamp;
//    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
//    if(fabs(howRecent) < 15.0){
//        [self updateMapMarkers];
//    }

}

.plist image

Big Kahuna
  • 149
  • 6
3

I am using XCode 12.5/IOS 14. This is what I have done to resolve the issue

  1. In XCode, go to menu "Product -> Scheme -> Edit scheme" OR press "Shift+Command+<"
  2. Click on "Run" from the left pane and then "Options"
  3. You will find "Core Location" field, make sure "Allow Location Simulation" is checked
  4. Also, do not leave "Default Location" drop-down to "None". Select any city
  5. Close. No need to restart XCode
Amit Baderia
  • 4,454
  • 3
  • 27
  • 19
1

I have these suggestions for people using apple maps in their app

  1. Don't relay on results of simulator, because always it will not give exact results.
  2. For all map related debugging and execution test on real devices.
  3. As in xcode 8(with my knowledge) you can just connect your iOS device to system and and run the app directly.
  4. Even for above problem the solution is same, just try with real devices your problem will be solved.
0

In my case I was assigning the return of CLLocationManager() to a variable inside of the ViewControllers viewDidLoad override.

As soon as that lifecycle method completes the object reference is released; dismissing the dialog before a user has a chance to respond, also causing issues with logic else where that may depend on that reference.

I just had to move the assignment out of the life cycle method override to the root of the class I was using it in.

Donald
  • 3,901
  • 1
  • 20
  • 15