Unuliar
Unuliar

Reputation: 27

Error when trying to get user location (swift)

I am trying to get user's location on iOS. I use CLLocationManagerDelegate for it.

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.first
    print("locations = \(location?.coordinate.latitude) \(location?.coordinate.longitude)")
}

But I get this error

2017-03-24 18:03:57.183 *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-2100.0.34/Framework/CoreLocation/CLLocationManager.m:867
2017-03-24 18:03:57.194 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'

After implementing locationManager:didFailWithError:

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

I have this warning

Instance method 'locationManager(:didFailWithError:)' nearly matches optional requirement 'locationManager(:didFailWithError:)' of protocol 'CLLocationManagerDelegate'

And again this error

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'

I dont know what incorrect. locationManager:didFailWithError: implemented, but I have same error.

Upvotes: 0

Views: 1217

Answers (1)

Unuliar
Unuliar

Reputation: 27

changing to

@objc(locationManager:didFailWithError:)
func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError) {
    print(error.localizedDescription)
}

helped in my situation

Upvotes: 0

Related Questions