简体   繁体   English

iOS 11.2.6更新后应用崩溃

[英]App crashing after iOS 11.2.6 update

The code for my CLLocationManagerDelegate was working fine before the last iOS update, however now it is crashing with the error 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:' 我的CLLocationManagerDelegate的代码在上次iOS更新之前运行良好,但是现在由于错误'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'而崩溃'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'

This is the code for my delegate (note: start() is called from my ViewController): 这是我的委托的代码(注意:从我的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
    }
}

Not sure how you're initializing this class, setting delegates, etc., but this is the UserLocation class that I use. 不确定如何初始化此类,设置委托等,但这是我使用的UserLocation类。 Compare it to yours. 与您的比较。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM