-1

I'm trying to request the user's location while using the app. I have added NSLocationWhenInUseUsage to my plist. The app doesn't crash or otherwise show any errors. The app just starts and there isn't any popup to request permission of the user.

import UIKit
import MapKit 
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    locationManager.requestWhenInUseAuthorization()

I also added this function to my code:

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

    println(locations)

Also, I'm not an adept programmer, so a simpler answer would be better.

Aj Langley
  • 127
  • 9

2 Answers2

0

there isn't any popup to request permission of the user

Even if you call locationManager.requestWhenInUseAuthorization(), there will be no dialog unless (1) location services are turned on, and (2) location authorization for this app is undetermined.

In other words, the dialog appears only if the user can meaningfully authorize. The user cannot authorize if location services are not available, if authorization has already been denied, or if this device is restricted.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So how do I check for the authorization status? I'm assuming I would make an if then statement before locationManager.requestWhenInUseAuthorization, but I don't know what commands to use to check for that (sorry, I'm not well versed on the vocabulary of how to refer to all the different parts of the code). – – Aj Langley Jun 17 '15 at 17:44
  • See my explanation here: http://stackoverflow.com/a/30495310/341994 – matt Jun 17 '15 at 17:51
  • This is written in objective c. I really don't understand enough to decipher this explanation. It would be awesome if I could find somewhere where I can just look at the code and learn it that way. – Aj Langley Jun 17 '15 at 19:20
  • What? My answer is not written in Objective-C or any language other than English. It explains the logic of what you need to do. Do that. – matt Jun 17 '15 at 19:55
0

As matt says in his answer, you need to first check the authorization status. If you get not determined, THEN you should call requestWhenInUseAuthorization.

Once you've done that you need to wait for a call to your delegate method locationManager:didChangeAuthorizationStatus. When the user finishes authorizing location services (or denying it) that delegate method will be called.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • So how do I check for the authorization status? I'm assuming I would make an if then statement before locationManager.requestWhenInUseAuthorization, but I don't know what commands to use to check for that (sorry, I'm not well versed on the vocabulary of how to refer to all the different parts of the code). – Aj Langley Jun 17 '15 at 17:04
  • You should really read the CLLocationManager class reference in the Xcode docs. There is a class method authorizationStatus that lets you query it. – Duncan C Jun 17 '15 at 17:40