Location permission check and authorization
Asked Answered
F

4

12

I want my program to display the users location on the map. It was working at first then randomly stopped so i added the code below to try to fix it but I'm having problems. Thanks in advance!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if CLLocationManager.locationServicesEnabled()
        {

            let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
            if status == CLAuthorizationStatus.NotDetermined
            {
                locationManager.requestAlwaysAuthorization()
            }
            } else {

                print("locationServices disenabled")
            }
            locationManager.startUpdatingLocation()
            self.locationManager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
            mapView.delegate = self
            centerMapOnLocation(initialLocation)
            addBoundry()
    }
Frenzy answered 2/8, 2016 at 17:24 Comment(4)
What problems do You have?Dann
We need more information regarding issues you are facing. Also I notice there are 2 calls to 'startUpdatingLocation' method of 'locationManager'.Handbarrow
Did you set the required field in info.plist?Dribble
This is the error I am getting: "Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first."Frenzy
E
19

You required to call,

locationManager.requestAlwaysAuthorization() 

as well

 locationManager.requestWhenInUseAuthorization()

to use location smoothly in foreground also!!!

and you should not need two instance to call startupdatinglocation. keep one. you should use instance or global variable instead of local to get location throughout the scope.

Update :

You have to set two keys in info.plist like, NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription with it's usage description.

Electrophorus answered 2/8, 2016 at 17:33 Comment(4)
Thanks I made those changes but I am still getting the error I commented aboveFrenzy
Note that if you let the description key blank, your app will be rejected by Apple (mine got 08/17)Monosyllable
You can't leave those plist descriptions blank anymore.Darsey
@Darsey : Yeah! We can't leave it blank in latest updates. Updated answer. Thanks for suggestion!Electrophorus
P
5

I was having the same issue, even with NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescriptionset in info.plist. My debugger told me to also set NSLocationAlwaysAndWhenInUseUsageDescription...so I did it. It worked!

Proportion answered 23/8, 2017 at 21:12 Comment(1)
This fixed it for me as well. I wish apple would stop moving decisions like this around from version to version.Curson
R
5

If you want the "WhenInUse" notification you should only add "Privacy - Location When In Use Usage Description" in your info.plist but if you want the "Always" access your info.plist should look like this info.plist screenshot:

Image

You don't have to add any request authorization like requestAlwaysAuthorization or requestWhenInUseAuthorization

Rundlet answered 30/7, 2018 at 0:30 Comment(0)
M
1

This is my code:

First import the library:

import CoreLocation

A little example:

        let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
        if status == CLAuthorizationStatus.notDetermined || status == CLAuthorizationStatus.denied || status == CLAuthorizationStatus.restricted
        {
            locationManager?.requestAlwaysAuthorization()
        }

And don`t forget to add these permissions in the Info.plist:

enter image description here

Molten answered 10/1, 2023 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.