CLLocationManager() keeps crashing my app

This is really stumping me and driving me nuts. I haven't been able to find a fix to this. When I click on the prompt to "Allow Access" the app immediately crashes on me. When I open the app up again "Allow Access" has already been selected and the app works fine. How do I fix the crashing? 😢 I think it has to do with the placement of locationManager.startUpdatingLocation() but I could be completely wrong.


I get the classic: fatal error: unexpectedly found nil while unwrapping an Optional value


import UIKit

import CloudKit

import CoreLocation

class GameInformationViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate, CLLocationManagerDelegate {

var locationManager = CLLocationManager()


override func viewDidLoad() {

super.viewDidLoad();

locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.delegate = self

locationManager.distanceFilter = 150.0

locationManager.requestWhenInUseAuthorization()

}


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

if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse)

{

locationManager.startUpdatingLocation()

}

else

{

locationManager.stopUpdatingLocation()

}

}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {

let alert = UIAlertController(title: "Alert: Cannot Locate", message: "There were problems obtaining your location. Please try again later.", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in

UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)

}))

self.presentViewController(alert, animated: true, completion: nil)

}


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


}


@IBAction func currentLocationPressed(sender: AnyObject) {


var currentLocation = CLLocation()


if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){

locationManager.startUpdatingLocation()

locationManager.stopUpdatingLocation()

currentLocation = locationManager.location!

}

else

{

locationManager.requestWhenInUseAuthorization()

if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse)

{

currentLocation = locationManager.location!

}

else

{

return

}

}


let longitude :CLLocationDegrees = currentLocation.coordinate.longitude

let latitude :CLLocationDegrees = currentLocation.coordinate.latitude


let location = CLLocation(latitude: latitude, longitude: longitude) /

print(location)


CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in

if (error != nil) {

print("Reverse geocoder failed with an error" + error!.localizedDescription)

} else if placemarks!.count > 0 {

let pm = placemarks![0] as CLPlacemark

self.gameLocation.text = pm.locality

} else {

print("Problems with the data received from geocoder.")

}

})

}


Answered by DTS Engineer in 113296022

If you run the app from the Home screen (rather than Xcode) and then trigger the crash, you should get a crash log which will let you see, at a minimum, the function that’s tripping over the nil check.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

If you run the app from the Home screen (rather than Xcode) and then trigger the crash, you should get a crash log which will let you see, at a minimum, the function that’s tripping over the nil check.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Where does the crash occur exactly ?

Why do you unwrap UIAlertAction!


alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
            UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)


Did you try with just :


alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (action in
            UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)

eskimo- What am I looking for in the crash logs? I didn't find anything indicating the function that it is tripping on. Walking through it line by line, the last line that it's trying to execute is locationManager.startUpdatingLocation(). I believe this is what is causing the crash.

CLLocationManager() keeps crashing my app
 
 
Q