Delegate Must Respond to Locationmanager:Didupdatelocations Swift Eroor

Delegate must respond to locationManager:didUpdateLocations swift eroor

You are using xcode8 and swift3 but your delegate methods are copied from swift 2.Change your delegate methods as below.

extension ViewController : CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error:: \(error.localizedDescription)")
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
}

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

if locations.first != nil {
print("location:: (location)")
}

}

}

Error: 'Delegate must respond to locationManager:didUpdateLocations:' when collecting user location

First, add explicitly that you confirm CLLocationManagerDelegate:

class LoginViewController: UIViewController, CLLocationManagerDelegate

Second, set up delegate property for CLLocationManager:

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

Third, in CLLocationManagerDelegate doc I see different from your declaration of didUpdateLocations and didFailWithError method:

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

Also, you have few other issues in code. They are all about unsafe unwrapping. You should not use unsafe unwrapping ! everywhere. Doing this you are killing optionals philosophy. Doing right thing, on other hand, can drastically increase stability of your app. In loginDidTouch function make following corrections:

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

When you call this function first time, your location not determined yet (it will be determined asynchronously, you will get location in delegate method), thats why you have fatal error when used force unwrapp. In didUpdateLocations function:

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

correction of this part of code is shorter then your original code, and prevents you from two possible crashes at one time - when placemarks is nil and when placemarks is not nil, but empty array

SwiftUI error - Delegate must respond to locationManager:didFailWithError:

func locationManager(_manager: CLLocationManager,

should be

func locationManager(_ manager: CLLocationManager,

Delegate must respond to locationManager:didFailWithError: even though implemented didFailWithError method

Aaaandd I realized it's b/c I needed to use an Error of a specific type (specifically Swift.Error)
This is the right method declaration for didFailWithError:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {

App crashes when using locationManager.requestLocation()

You may need to implement

func locationManager(_ manager: CLLocationManager, 
didFailWithError error: Error) { --- }

Delegate method of CLLocationManagerDelegate

Why is locationManager (:didUpdateLocations) not called

You need to make

let locManager = CLLocationManager()

an instance var to hold a strong reference for the delegate , also you either need 1 of these

locManager.startUpdatingLocation()
locManager.requestLocation()


Related Topics



Leave a reply



Submit