1

Location is not being printed to console. I have had this exact code working before but since updating to Xcode 10 it is not print the users current location into the console.

var locationManager: CLLocationManager!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyCurrentLocation()
}


func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")

}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

Info.plist: enter image description here

Dinsen
  • 2,139
  • 3
  • 19
  • 25
  • In the code above? –  Oct 24 '18 at 00:53
  • Welcome to Stack Overflow, this question appears to have already been answered [here](https://stackoverflow.com/a/25698536/1361672) – RLoniello Oct 24 '18 at 00:56
  • I have already tried this, The location is still not being updated –  Oct 24 '18 at 01:11
  • literally nothing –  Oct 24 '18 at 01:35
  • The code you provided works when added to a new project, ensure your `info.plist` is set correctly and the application has access to location services: `Settings > Privacy > Location Services > [Your App Name]` is set to "Always" – RLoniello Oct 24 '18 at 01:47
  • updated with a photo –  Oct 24 '18 at 02:05
  • @matt I placed print statements in all 3 and all of them returned the print statement. –  Oct 24 '18 at 02:30
  • @matt I also placed a break point on locationManager.startUpdatingLocation and it is being called –  Oct 24 '18 at 02:37
  • @matt Yes I have –  Oct 24 '18 at 02:51
  • I just opened a different emulator and when my view controller load it didn't ask for authorisation, Should it? –  Oct 24 '18 at 02:54
  • 1
    @Gary Host Its better to test location services in a real device rather than simulators. If using simulator, you can simulate a location via : Debug > Location > Custom Location.. – Sujal Oct 24 '18 at 04:24
  • @Sujal I have been doing both to no success –  Oct 24 '18 at 04:46
  • Are you importing CoreLocation? – Jacob Ahlberg Oct 24 '18 at 07:29

4 Answers4

2

The problem is your Info.plist. You need all three keys:

enter image description here

Fix that and the app will spring to life. I pasted your code into a project, set up the Info.plist as shown, and it worked fine.

You can actually omit "Always" starting in iOS 11, but you must have both "When In Use" and "Always and When In Use", even if you are planning to ask only for "Always".

The runtime was in fact telling you this all along in the Xcode console, but you apparently weren't looking at it. It says:

The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    for just _requestAlwaysAuthorization_ why does he need all 3?! – mfaani Oct 24 '18 at 17:42
  • @Honey Don't look at me! I tried it with just Always and When In Use and got an error. Then I tried after adding Always and got an error. Then I added When In Use and the whole app sprang to life, as I said. – matt Oct 24 '18 at 17:51
  • This issue happens *only* for Xcode 10 right? Nonetheless I guess it never bothers to add them all :) – mfaani Oct 24 '18 at 17:58
  • @Honey You can actually omit "Always" starting in iOS 11, but you must have both "When In Use" and "Always and When In Use". It has nothing to do with the Xcode version; it has to do with the iOS version. – matt Oct 24 '18 at 18:11
1

Have you imported CoreLocation?

Try to change your locationManager form

var locationManager: CLLocationManager!

to

let locationManager = CLLocationManager()

and then you don't need it in determineMyCurrentLocation()

I would also let userLocation = CLLocation() in the top of your class and then update your location.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
    let newLocation = locations[0]
    let distance = userLocation.distance(from: newLocation)
    // using this to update my location every 100m
    if distance > 100 {
        userLocation = newLocation
    }

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")
}
Daniel Larsson
  • 527
  • 2
  • 6
  • 23
1

Step:1 - import

import MapKit
import CoreLocation

Step:2 - use : MKMapViewDelegate,CLLocationManagerDelegate

Step:3 -

var locationManager = CLLocationManager()
var latitude: Double = 0.0
var longitude: Double = 0.0

Step:4 - In ViewDidLoad

 self.determineMyCurrentLocation()

Step:5 - define Func

func determineMyCurrentLocation() {
    if CLLocationManager.locationServicesEnabled()
    {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.distanceFilter = kCLDistanceFilterNone
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        if (authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways)
        {
            self.locationManager.startUpdatingLocation()
        }
        else if (locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)))
        {
             self.locationManager.requestAlwaysAuthorization()
        }
        else
        {
            self.locationManager.startUpdatingLocation()
        }
    }
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error while requesting new coordinates")
    }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.latitude = locations[0].coordinate.latitude
    self.longitude = locations[0].coordinate.longitude
}

Insert in info.plist {Privacy - Location Usage Description , Privacy - Location Always and When In Use Usage Description ,Privacy - Location When In Use Usage Description , Privacy - Location Always Usage Description}

Tej Patel
  • 119
  • 2
  • 5
0

Confirm NSLocationWhenInUseUsageDescription and NSLocationAlwaysUseUsageDescription are in your plist file.

M.G.
  • 44
  • 5