gpt4 book ai didi

ios - 仅在首次启动时出现 requestWhenInUseAuthorization 错误

转载 作者:行者123 更新时间:2023-11-29 01:14:42 25 4
gpt4 key购买 nike

错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

代码:

class ViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()

let currentLocationCoo = locationManager.location?.coordinate
if let locationCoo = currentLocationCoo{
mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: locationCoo.latitude, longitude: locationCoo.longitude), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
print("location ni nil")
}else{


mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: 52.05695, longitude: 14.50575), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
}

let defaultAnnotation = MKPointAnnotation()
defaultAnnotation.coordinate = mapView.region.center
defaultAnnotation.title="Center"
defaultAnnotation.subtitle="This is!"
mapView.addAnnotation(defaultAnnotation)

mapView.showsUserLocation=true
mapView.showsBuildings=true
mapView.userTrackingMode = .None

}

我注意到 iOS 仅在首次启动时抛出错误。在我什至同意位置服务提示之前。在随后的发布中,它工作正常。是因为应用程序启动的第一个 View Controller 已经使用了 map 吗?解决方法可能不是直接跳转到 map ,而是先显示一些其他 VC。还有其他解决方法吗?

最佳答案

是的,您是对的,mapView.showsUserLocation=true 需要访问您的当前位置,如果不同意操作系统显示的警报,这是不可能的。您可以实现 CLLocationManagerDelegatedidChangeAuthorizationStatus 方法,并在其中包含与 map 相关的代码。尝试以下代码。

class ViewController: UIViewController,CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .AuthorizedWhenInUse:
updateMap()
break
case .AuthorizedAlways:
updateMap()
// write your map related code here
break
default:
break
}
}

func updateMap() {
let currentLocationCoo = locationManager.location?.coordinate
if let locationCoo = currentLocationCoo{
mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: locationCoo.latitude, longitude: locationCoo.longitude), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
print("location ni nil")
}else{
mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: 52.05695, longitude: 14.50575), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
}

let defaultAnnotation = MKPointAnnotation()
defaultAnnotation.coordinate = mapView.region.center
defaultAnnotation.title="Center"
defaultAnnotation.subtitle="This is!"
mapView.addAnnotation(defaultAnnotation)

mapView.showsUserLocation=true
mapView.showsBuildings=true
mapView.userTrackingMode = .None
}
}

关于ios - 仅在首次启动时出现 requestWhenInUseAuthorization 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347114/

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