Error: "The delegate must respond to locationManager: didUpdateLocations: 'while collecting user location

So, I want to collect the location of the user when the user clicks the button and uses that location later in my application. With my current implementation, when the button is pressed, the user should be asked to allow the location for sharing, and then they should enter the application (anonymously through Firebase, if you're interested), and the location information of the users should be printed to the console. The request works, however, after clicking the location resolution button, my application terminatesdue to uncaught exception 'NSInternalInconsistencyException'

and the reason is because 'Delegate must respond to locationManager:didUpdateLocations:'

This is strange because there is a function in my code didUpdateLocations:.

I have no idea what is happening and any help is appreciated. My ViewController code is below, thanks!

/*
* Copyright (c) 2016 Ahad Sheriff
*
*/

import UIKit
import Firebase
import CoreLocation

class LoginViewController: UIViewController {

    // MARK: Properties
    var ref: FIRDatabaseReference! // 1
    var userID: String = ""

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference() // 2
    }

    @IBAction func loginDidTouch(sender: AnyObject) {

        //ask user to enable location services
        locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            //collect user location
            locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
            locationManager.requestLocation()
            locationManager.startUpdatingLocation()

            let location = self.locationManager.location

            var latitude: Double = location!.coordinate.latitude
            var longitude: Double = location!.coordinate.longitude

            print("current latitude :: \(latitude)")
            print("current longitude :: \(longitude)")

            //Log in to application anonymously using Firebase
            FIRAuth.auth()?.signInAnonymouslyWithCompletion() { (user, error) in
                if let user = user {
                    print("User is signed in with uid: ", user.uid)
                    self.userID = user.uid
                } else {
                    print("No user is signed in.")
                }

                self.performSegueWithIdentifier("LoginToChat", sender: nil)

            }

        }

        else {
            print("Unable to determine location")
        }

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        super.prepareForSegue(segue, sender: sender)
        let navVc = segue.destinationViewController as! UINavigationController // 1
        let chatVc = navVc.viewControllers.first as! ChatViewController // 2
        chatVc.senderId = userID // 3
        chatVc.senderDisplayName = "" // 4
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
    {
        //--- CLGeocode to get address of current location ---//
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

            if (error != nil)
            {
                print("Reverse geocoder failed with error" + error!.localizedDescription)
                return
            }

            if placemarks!.count > 0
            {
                let pm = placemarks![0] as CLPlacemark
                self.displayLocationInfo(pm)
            }
            else
            {
                print("Problem with the data received from geocoder")
            }
        })

    }


    func displayLocationInfo(placemark: CLPlacemark?)
    {
        if let containsPlacemark = placemark
        {
            //stop updating location
            locationManager.stopUpdatingLocation()

            let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
            let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
            let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
            let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""

            print(locality)
            print(postalCode)
            print(administrativeArea)
            print(country)
        }

    }


    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        print("Error while updating location " + error.localizedDescription)
    }


}
+4
source share
1 answer

First add explicitly that you confirm CLLocationManagerDelegate:

class LoginViewController: UIViewController, CLLocationManagerDelegate

Second, set the delegation property to CLLocationManager:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    ref = FIRDatabase.database().reference()
}

Thirdly, in the CLLocationManagerDelegatedoc, I see a difference from your method declaration didUpdateLocationsand didFailWithError:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError)

, . . ! . , . , . loginDidTouch :

var latitude: Double? = location?.coordinate.latitude
var longitude: Double? = location?.coordinate.longitude

, ( , ), . didUpdateLocations:

if let pm = placemarks?.first
{
    self.displayLocationInfo(pm)  
}

, , - placemarks , placemarks ,

+9

All Articles