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

时间:2019-01-09 14:48:32

标签: ios swift core-location cllocationmanager

我有此代码:

In [222]: df.columns.tolist()
Out[222]: ['A', 'B', 'C']

In [218]: for i in df.columns:
     ...:     print(i)
     ...:     ## do your stuff
A
B
C

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()时,应用程序将崩溃:

  

2019-01-09 15:48:36.585518 + 0100 GetLocation [1206:248754] ***断言   -[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] ***   由于未捕获的异常而终止应用程序   “ NSInternalInconsistencyException”,原因:“代理必须响应   locationManager:didFailWithError:'

     

***首先抛出调用堆栈:

     

(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:以类型未捕获的异常终止   NSException

     

(lldb)

为什么呢?我在做什么错了?

3 个答案:

答案 0 :(得分:2)

您可能需要实施

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

CLLocationManagerDelegate的委托方法

答案 1 :(得分:0)

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

答案 2 :(得分:0)

清楚地说明了原因。

  

原因:“代理必须响应locationManager:didFailWithError:”

您必须实现

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

}

由于请求位置是异步发生的,因此需要时间。 当位置在“位置管理器”中时,它将通过locationManager:didUpdateLocations:

通知用户