使用 locationManager.requestLocation() 时应用崩溃

App crashes when using locationManager.requestLocation()

我有这个代码:

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()
    }
}

当在 UIButton 上点击时,IBAction 被调用并且当前用户的位置显示在控制台中。

因为我只需要获取一次位置,所以我想使用locationManager.requestLocation()

但是当删除 locationManager.stopUpdatingLocation() 并将 locationManager.startUpdatingLocation() 替换为 locationManager.requestLocation() 时,应用程序会在按下 UIButton 时崩溃,并在控制台中显示以下日志:

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)

为什么?我做错了什么?

您可能需要实施

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

CLLocationManagerDelegate

的委托方法

错误信息很清楚。您需要实现另一个委托方法 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
    }
}

原因描述的很清楚

reason: 'Delegate must respond to locationManager:didFailWithError:'

您必须实施

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

}

由于请求位置是异步发生的,因此需要时间。 当位置与位置管理器一起使用时,它会通过 locationManager:didUpdateLocations:

通知用户