应用程序在iOS 11.2.6更新后崩溃

时间:2018-02-23 02:17:59

标签: ios swift core-location

我的CLLocationManagerDelegate的代码在上次iOS更新之前工作正常,但现在崩溃时出现错误'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'

这是我的代理人的代码(注意:从我的ViewController调用start()):

class Location: NSObject, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    func start() {
        locationManager.delegate = self

        if locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)) {
            locationManager.requestAlwaysAuthorization()
        } else {
            startLocationUpdates()
        }
    }

    func startLocationUpdates() {
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.activityType = .automotiveNavigation
        locationManager.startUpdatingLocation()
        locationManager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse || status == .authorizedAlways {
            startLocationUpdates()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // snip
    }

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

1 个答案:

答案 0 :(得分:0)

不确定如何初始化此类,设置委托等,但这是我使用的UserLocation类。与你的比较。

import Foundation
import CoreLocation

var userLocation: UserLocation?

class UserLocation: NSObject, CLLocationManagerDelegate {

    var launchLocationSet = false

    override init() {
        super.init()
        setupLocationManager()
    }

    public var currentLocation: CLLocation!

    private var locationManager = CLLocationManager()

    private func setupLocationManager() {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.delegate = self
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        currentLocation = locations.last! as CLLocation!
        if !launchLocationSet {

            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateLocation"), object: nil, userInfo: nil)
            launchLocationSet = true

        }
    }

    func currentLatitude() -> CLLocationDegrees {
        return currentLocation.coordinate.latitude
    }

    func currentLongitude() -> CLLocationDegrees {
        return currentLocation.coordinate.longitude
    }
}