Get Location fails after using other functions

I have finished the My Locations app upto the last section.

If I start the App and do get locations, it finds my location. Then I can press Stop, then get my locations again and it works.

If I goto Map, pick a location, then get info for that location, then go back to tag a location, stop the service and press get my location, it searches and then times out. It fails with
didFailWithError Error Domain=kCLErrorDomain Code=0 “(null)”

Then pressing get my location never works again, until I restart the App.

I have compared the code tot he tutorial code and can’t find any real differences and everything else works. I put in print statements and they seem to track what is going on, but it doesn’t explain why it keeps timing out.

Is this on the simulator or on a device? Since iOS 9, the simulator has been behaving strangely for me with “fake” locations. But on a real device this definitely shouldn’t happen – if it does, you’ve found a bug somewhere. :slightly_smiling:

I havent looked at the code itself, but Id bet that you are stopping the locationmanager service at some point (perhaps when you press “stop” wherever that is.

Hunt it down by looking for instances of locationmanager and find out where you stop it. It works when you run the app again because you surely have a method that gets called that starts it back up again on launch.

I added more print statements and there is a function locationManager(didFailWithError) which gets called and it has an exit based on the error code where is doesn’t do anything else.

However, pressing Stop, then GetMyLocations again should restart everything, but it keeps erroring out. Do we need to clear the error code somehow before restarting?

Clearing the error code happens in the getLocation() IBAction method, so double-check to see if everything is there.

If you still can’t get it to work, zip up your project and upload it somewhere, and I can take a look.

I see clearing the global variable, lastLocationError, but I was wondering if the NSError clears automatically when you read it, or do you have to do something to clear it, otherwise you keep getting the same error.

Also in this code, if the error is location unknown, it doesn’t set the lastLocationError variable.

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(“didFailWithError (error)”)

if error.code == CLError.LocationUnknown.rawValue {
  return
}
lastLocationError = error

I took the 06 - Photo Picker code from the tutorials and built it for my Ipad Mini 4 and it behaves the same way, and I basically see the same type error message, so you should be able to duplicate the problem using your own source code.

Somewhat different for me, I don’t get locations at all- this was after setting the plist, locationManager delegate functions, and after implementing authStatus == .denied This is for the Swift 4 version

Can you uploaded a ZIPped copy of your project somewhere and provide a link so that I can take a look?

Thank you, fahim I appreciate it!
Link here

OK, the issue was on line 39 of your CurrentLocationViewController.swift file :slight_smile: You have:

locationManager.stopUpdatingLocation()

But what you really need is:

locationManager.startUpdatingLocation()

My guess would be that you selected the wrong item when using code completion :slight_smile: Once you fix that, your code should work fine.

1 Like

Aiya! What a goof I am for overlooking that! :joy:

Thank you very much for your time and help in this matter :blush: