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
Why Is Casting a Struct to Anyobject Not a Compile Error in Swift
Cannot Call Value of Non-Function Type 'Nshttpurlresponse' Alamofire Objectmapper
Self' Is Only Available in a Protocol or as the Result of a Class Method
How to Convert Unmanaged<Cfdata> to Nsdata
How to Create a Directory in Downloads Folder with Swift on MACos? Permission Exception
Check If Color Is Blue(Ish), Red(Ish), Green(Ish),
Generate Random Number of Certain Amount of Digits
How to Remove Space Above and Below Text View in Swiftui
Lottieanimationview Size Won't Change/Is Too Small (Ios/Swift)
Stack Overflow When Defining Subscript on Ckrecord in Swift
Navigationview Swiftui Shows Split View in iPad
How to Check If a Type Is Optional in Swift
How to Convert a Unsafemutablepointer<Void> to Uint8
Get the String Up to a Specific Character