-1

I build a quick demo app with a code taken from here: https://stackoverflow.com/a/35685278/3766930

The code that I used is this:

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var mapView: MKMapView!
    var locationManager: CLLocationManager?

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager = CLLocationManager()
        locationManager!.delegate = self

        if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
            locationManager!.startUpdatingLocation()
        } else {
            locationManager!.requestWhenInUseAuthorization()
        }
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {


        switch status {
        case .NotDetermined:
            print("NotDetermined")
        case .Restricted:
            print("Restricted")
        case .Denied:
            print("Denied")
        case .AuthorizedAlways:
            print("AuthorizedAlways")
        case .AuthorizedWhenInUse:
            print("AuthorizedWhenInUse")
            locationManager!.startUpdatingLocation()
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let location = locations.first!
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
        mapView.setRegion(coordinateRegion, animated: true)
        locationManager?.stopUpdatingLocation()
        locationManager = nil
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Failed to initialize GPS: ", error.description)
    }
}

I also added permission to my plist file:

NSLocationWhenInUseUsageDescription

But when I deployed the app on my phone and ran it - it didn't ask me for any permission and in the log file I only got:

NotDetermined

How can I force my app to always use the gps coordinates when user starts it? I want to introduce maybe a check at the very beginning of the app that checks if GPS is enabled, and if not - avoid running the app and just prompt the user to turn on GPS data. Is that possible and achievable in Swift?

Community
  • 1
  • 1
user3766930
  • 5,629
  • 10
  • 51
  • 104

2 Answers2

1

What is AuthorizedAlways?

Since you are asking for When In Use permission, your info.plist needs to have a string value for NSLocationWhenInUseUsageDescription

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Oh yeah, sorry, it was my mistake when writing this question, of course I added the string you suggested instead of `AuthorizedAlways`. I'm gonna edit my original question. – user3766930 Feb 28 '16 at 20:50
  • Can you edit your question to show your info.plist? Have you tried deleting the app from your device/simulator? Have you checked to see that location services are enabled in settings for your device and your app? I just used your same code and it worked – Paulw11 Feb 28 '16 at 20:54
  • so when you deploy this code into the phone and run it for the first time it asks you for gps permission? – user3766930 Feb 28 '16 at 20:57
  • Yes. It prompted me for permission with the text of the `NSLocationWhenInUseUsageKey` – Paulw11 Feb 28 '16 at 20:57
  • Ok, so that clears a lot, thank you for that test, now I just want to figure out how to disable running the app if the gps is disabled. Can you help me with that and suggest how should I modify the code above? – user3766930 Feb 28 '16 at 21:03
  • If you get an authorisation status of `denied` then you can display an alert asking the user to activate location services but you can't prevent the app from running as such. – Paulw11 Feb 28 '16 at 21:43
0

On iOS you don't have the option of choosing explicitly the GPS radio, but you can choose a desired accuracy that will turn on that radio.

locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation

This will start your CLLocationManager with GPS.

Take into consideration that GPS will take a couple seconds to give you the most accurate location, so you can filter out any locations coming to the locationManager:didUpdateLocations: method that are higher that 20.0 or any value you think is too innacurate.

quant24
  • 393
  • 6
  • 22