2

My very first app is working fine so far, if location services are allowed for it.

As soon as I disable the location services for this app in particular (Airplane mode, as well as generally disabled location services are working as expected).

The Error at runtime

The code is the following:

    func locationServices()->Bool{
    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .NotDetermined, .Restricted, .Denied:
            return false
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            return true
        }
    } else {
        return false
    }

}

Called from the viewDidLoad:

    override func viewDidLoad() {
    super.viewDidLoad()

    txtNotes.delegate = self
    datePicker.maximumDate = NSDate()
    if (inc != nil) {
        let Dateformatter = NSDateFormatter()
        Dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
        navigationItem.title = Dateformatter.stringFromDate((inc?.date)!)
        datePicker.date = (inc?.date)!
        txtNotes.text = inc?.notes
        ratingControl.rating = (inc?.rating)!
        lblAccuracy.text = inc?.geoloc
        self.location = inc?.geocor
    }
    else {
        if(locationServices()){
        self.locationManager = CLLocationManager()
        locationManager!.delegate = self
        locationManager!.requestWhenInUseAuthorization()
        locationManager!.desiredAccuracy = kCLLocationAccuracyBest
        locationManager!.startUpdatingLocation()

        }
        else{
            lblAccuracy.text = "Location Services Disabled"
            let alertController = UIAlertController(
                title: "Location Services Disabled",
                message: "Please allow this application to use location services",
                preferredStyle: .Alert)

            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)

            let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
                if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                    UIApplication.sharedApplication().openURL(url)
                }
            }
            alertController.addAction(openAction)
            self.presentViewController(alertController, animated: true, completion: nil)
            viewDidLoad()
        }
    }
}
Michael
  • 289
  • 2
  • 5
  • 14

1 Answers1

0

Accessing a not yet initialized delegate is not a smart idea...

Initializing prior to accessing it resolved my issue.

Michael
  • 289
  • 2
  • 5
  • 14