Я пытаюсь открыть контроллер представления, который требует доступа к местоположению пользователя.В настоящее время у меня работает код ниже.Это, однако, только запрашивает у пользователя разрешения при первом открытии представления.После этого, если пользователь отказал, приложение вылетает.
Я запускаю эту функцию в начале моего viewDidLoad (), а затем я запускаю
centerMapOnLocation(CLLocationManager().location!, mapView: mapView)
, где эта строка кодапроисходит сбой с сообщением об ошибке:
Поток 1: неустранимая ошибка: неожиданно обнаружен ноль при развертывании необязательного значения
Что вызывает это?
func askForPermission() {
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() == true {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
else {
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
}
}
Я также пытался запустить следующую функцию, и в том же месте произошел сбой.
func checkAuthorizationStatus() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
break
case .denied:
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
break
case .restricted:
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
break
case .authorizedAlways: break
}
}
Как я могу это исправить?