Search code examples
swiftmacosmapkitcore-locationswift4.2

location services prompt/authorization not activated in macos / swift


unable to obtain latitude and longitude values of current location in swift 4 for macos app instead I am receiving an error message in the console

"The connection to service named com.apple.locationd.desktop.synchronous was invalidated." UserInfo={NSDebugDescription=The connection to service named com.apple.locationd.desktop.synchronous was invalidated.}

Registration timer expired, but client is still registering!

class ViewController: NSViewController, CLLocationManagerDelegate 
{

let locationManager = CLLocationManager()
}


 override func viewDidLoad(){

       super.viewDidLoad()
      locationManager.delegate = self
       locationManager.desiredAccuracy = kCLLocationAccuracyBest
       locationManager.startUpdatingLocation()
        print (locationManager.location?.coordinate.latitude ?? 0);
        print (locationManager.location?.coordinate.longitude ?? 0);
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         print (locationManager.location?.coordinate.latitude ?? 0);
         print (locationManager.location?.coordinate.longitude ?? 0);
     }

i aslo enabled privacy checks in info.plist

instead of getting the coordinates values in the console I am getting the error message as above. location services enabled on the mac. what's wrong in the code?


Solution

  • This error message means that the application has not been granted permission to obtain the user's location. To fix this you need to ensure that the application is properly "sandboxed" and that the location is enabled. You can do this in Xcode with the following steps:

    1. In the project navigator, click on your application's target. This should bring up a view with tabs such as "General", "Capabilities", "Resource Tags", etc.
    2. Click on the "Capabilities" tab. This will give you a list of items such as "App Groups", "App Sandbox" and so on. Each item will have an "On/Off" button.
    3. Turn on the "App Sandbox" item and press the ">" button on the left to show the sandbox stuff.
    4. In the "App Data" section, select "Location".

    Now the next time you run your application, it will ask they user to allow it to use the current location. Assuming they answer in the affirmative, you should no longer see the error but instead should see the current position highlighted on the map.