Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager on macOS app does not ask for location

I am trying to set up location services on a macOS app and at the moment I can't get the request prompt to come up.

I just have this code in my viewDidLoad but nothing is coming up. I was / am under the impression that the .startUpdatingLocation() is called that it should ask for the permissions right?

At the moment I am only trying to get my own location - yes it is enabled in the settings to ask for them.

override func viewDidLoad() {
    super.viewDidLoad()

    let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    let status = CLLocationManager.authorizationStatus()
    if status == .restricted || status == .denied {

        print("Location Denied")

        return
    }
    else if status == .notDetermined {

        print("Show ask for location")

        return
    }
    else if status == .authorized {
        print("This should work?")

        return
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    currentLocation = locations[locations.count - 1]
    print("\(currentLocation)")
}

Do I need to ask for anything else?

like image 850
jwknz Avatar asked Sep 10 '17 08:09

jwknz


1 Answers

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:

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.

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.

Turn on the "App Sandbox" item and press the ">" button on the left to show the sandbox stuff.

In the "App Data" section, select "Location".

Now the next time you run your application, it will ask the 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.

like image 89
Ankur Prakash Avatar answered Sep 28 '22 03:09

Ankur Prakash