【问题标题】:"Thread 1: signal Sigabrt" when calling requestLocation()调用 requestLocation() 时的“线程 1:信号 Sigabrt”
【发布时间】:2019-05-14 04:50:54
【问题描述】:

尝试使用CLlocationManager().requestLocation() 获取用户的当前位置。但是应用程序崩溃并显示信号 Sigabrt。我知道这可能是因为未连接的插座,所以我确保它们都已连接但编译器仍然会弹出此警告。我已经正确实施 顺便说一下 p.list 中的键。

有趣的是,当我将 requestLocation() 更改为 startUpdatingLocation() 时,一切正常。

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestLocation()
    }
}

我希望有人能告诉我我做错了什么,或者我应该将 requestLocation() 替换为 startUpdatingLocation()。

【问题讨论】:

    标签: swift


    【解决方案1】:

    Apple 在documentation 中声明,如果您希望使用requestLocation(),则必须实现这些委托方法:

    locationManager(_:didUpdateLocations:)locationManager(_:didFailWithError:)

    下面的代码对我有用。

    import UIKit
    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
    
        let locationManager = CLLocationManager()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            if CLLocationManager.locationServicesEnabled() {
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.requestLocation()
            }
    
        }
    
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            print("LOCATIONS: \(locations)")
        }
    
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print("ERROR: \(error)")
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多