stack-flo stack-flo - 1 month ago 9
iOS Question

Alert Box Asking User to Grant Location Privileges is not showing

I am just getting started with SWIFT programming on iOS. However I have good experience in other languages such as JAVA, C# and PHP. That said, I am running into a little bit of a problem. I am creating a simple application that will show a user information based on their geographical location. I am following the tutorial listed here.

However, since a lot of things have changed between Swift 2.x and 3.x I have had to modify his code. Not too hard and so far I have gotten it to compile. However, I am never asked by the application to allow me to use the device's location.

I have followed the directions Apple listed here about putting "NSLocationAlwaysUsageDescription" in the Info.plist. However I am never prompted. The relevant piece of code that should be showing this alert is listed below.

Any help would be greatly appreciated. Thanks.

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// 1. status is not determined
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestAlwaysAuthorization()
}
// 2. authorization were denied
else if CLLocationManager.authorizationStatus() == .denied {
let controller = UIAlertController(title: "Error!", message: "Location services were previously denied. Please enable location services for this app in Settings.", preferredStyle: .alert)

let alertAction = UIAlertAction(title: "Dismiss", style: .destructive) { (action) in
print("Dismiss button tapped!")
}

controller.addAction(alertAction)

}
// 3. we do have authorization
else if CLLocationManager.authorizationStatus() == .authorizedAlways {
locationManager.startUpdatingLocation()
}
}

Answer

Your code is working fine you just need to present your alert:

self.present(controller, animated: true, completion: nil)

And also take a look at my answer here how you can use switch for this types of code.

Comments