gpt4 book ai didi

ios - iOS 11.2.6 更新后应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 11:45:02 25 4
gpt4 key购买 nike

我的 CLLocationManagerDelegate 代码在上次 iOS 更新之前工作正常,但现在崩溃并出现错误 'NSInternalInconsistencyException',原因:'Delegate 必须响应 locationManager:didUpdateLocations:'

这是我的委托(delegate)的代码(注意:start() 是从我的 ViewController 调用的):

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

最佳答案

不确定如何初始化此类、设置委托(delegate)等,但这是我使用的 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
}
}

关于ios - iOS 11.2.6 更新后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com