gpt4 book ai didi

swift - 调用 requestWhenInUseAuthorization 获取位置权限时出错

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

我正在尝试使用位置管理器检索用户在应用程序中的位置;正如苹果文档中所解释的,我创建了以下方法:

func startReceivingLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedWhenInUse && authorizationStatus != .authorizedAlways {
locationManager.requestWhenInUseAuthorization()
startReceivingLocationChanges()
return
}

if !CLLocationManager.locationServicesEnabled() {
displayError(withTitle: "Location Not Available", withDescription: "Enable Location Services at Settings > Privacy > Location Services", sender: self)
return
}

locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100.0 // In meters.
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.activityType = .other
locationManager.delegate = self
locationManager.startUpdatingLocation()
}

但是当我启动应用程序时,此崩溃在行附近显示错误“线程 1:EXC_BAD_ACCESS(代码=2,地址=0x16f0a7f60)”:

locationManager.requestWhenInUseAuthorization()

我指定我在 info.plist 中添加了相关的“隐私 - 位置始终和使用时使用说明”和“隐私 - 位置使用时使用说明”键。

有谁知道这个问题是什么原因造成的吗?谢谢。

最佳答案

您的代码中存在一些错误,例如递归,以防用户在使用时未授予权限,因此请查看我的代码。假设您在 info.plist 中添加了“隐私 - 位置始终和使用时使用说明”和“隐私 - 使用时位置使用说明”键。下面的代码工作完美,没有任何问题,苹果推荐。

import Foundation
import CoreLocation

typealias LocateMeCallback = (_ location: CLLocation?) -> Void

class LocationTracker: NSObject {

static let shared = LocationTracker()

var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.activityType = .automotiveNavigation
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.distanceFilter = 10

return locationManager
}()

var locateMeCallback: LocateMeCallback?
var currentLocation: CLLocation?
var isCurrentLocationAvailable: Bool {
return currentLocation != nil
}

func enableLocationServices() {
locationManager.delegate = self
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
// Disable location features
print("Fail permission to get current location of user")
case .authorizedWhenInUse:
// Enable basic location features
enableMyWhenInUseFeatures()
case .authorizedAlways:
// Enable any of your app's location features
enableMyAlwaysFeatures()
}
}

func enableMyWhenInUseFeatures() {
locationManager.startUpdatingLocation()
}

func enableMyAlwaysFeatures() {
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.startUpdatingLocation()
}

func locateMe(callback: @escaping LocateMeCallback) {
self.locateMeCallback = callback
enableLocationServices()
}

private override init() {}

}


// MARK: - CLLocationManagerDelegate
extension LocationTracker: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
print("locations = \(location.coordinate.latitude) \(location.coordinate.longitude)")
locateMeCallback?(location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
enableLocationServices()
}
}

用法

LocationTracker.shared.locateMe {  location in
guard let location = location else {
print("Cann't retrieve current location of user")
return
}
// do what ever you want to do with location
}

关于swift - 调用 requestWhenInUseAuthorization 获取位置权限时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52041381/

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