Reputation: 3318
I have this code:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
self.locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
super.viewDidLoad()
}
@IBAction func sendLocation() {
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let coordinate = locations.last?.coordinate else { return }
print(String(coordinate.latitude))
locationManager.stopUpdatingLocation()
}
}
When clinking on the UIButton
, the IBAction
gets called and the current users location is shown in the console.
Since I need to get the location only once, I would like to use locationManager.requestLocation()
.
But when removing locationManager.stopUpdatingLocation()
and replacing locationManager.startUpdatingLocation()
with locationManager.requestLocation()
, the app crashes when pressing the UIButton
with following logs in the console:
2019-01-09 15:48:36.585518+0100 GetLocation[1206:248754] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework/CoreLocation-2245.8.25/Framework/CoreLocation/CLLocationManager.m:897
2019-01-09 15:48:36.586443+0100 GetLocation[1206:248754] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'
*** First throw call stack:
(0x1b4e08ec4 0x1b3fd9a50 0x1b4d1eb3c 0x1b580d1d0 0x1bbcd8280 0x104690028 0x10469005c 0x1e20f23f8 0x1e1b7ffb8 0x1e1b802d8 0x1e1b7f2d8 0x1e212bb50 0x1e212cdb4 0x1e210c0b0 0x1e21daf1c 0x1e21dd914 0x1e21d6404 0x1b4d991f0 0x1b4d99170 0x1b4d98a54 0x1b4d93920 0x1b4d931f0 0x1b700c584 0x1e20f0ce4 0x104691564 0x1b4852bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Why that? What am I doing wrong?
Upvotes: 1
Views: 2872
Reputation: 16160
The reason is clearly described.
reason: 'Delegate must respond to locationManager:didFailWithError:'
You must implement
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
As the requesting location takes place asynchronously, it will take time.
When the location is with Location Manger, it notify user via locationManager:didUpdateLocations:
Upvotes: 0
Reputation: 54706
The error message is pretty clear. You need to implement the other delegate method, locationManager(_:, didFailWithError:)
.
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
self.locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
super.viewDidLoad()
}
@IBAction func sendLocation() {
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let coordinate = locations.last?.coordinate else { return }
print(String(coordinate.latitude))
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// handle the error
}
}
Upvotes: 0
Reputation: 100503
You may need to implement
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) { --- }
Delegate method of CLLocationManagerDelegate
Upvotes: 3