gpt4 book ai didi

ios - 使用 CLLocationManager 获取用户位置时出错

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

我正在使用 Collection View 和 Collection View 可重用 header 。在标题中,我试图获取用户的位置。我不断收到此错误:

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

我看了这个问题:Location Services not working in iOS 8并且所提出的解决方案都不适合我。我已将 key ( NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription )添加到我的 info.Plist 中,但这没有帮助。该应用程序从不请求用户获取其位置。这是我的代码:

编辑:我现在的代码:

   import UIKit
import MapKit
import Parse
import ParseUI
import CoreLocation

class ReusableHeaderMap: UICollectionReusableView, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

@IBOutlet weak var newMap: MKMapView!


override func awakeFromNib() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.Denied {

}
else if status == CLAuthorizationStatus.NotDetermined {
self.locationManager.requestAlwaysAuthorization()
}
else if status == CLAuthorizationStatus.AuthorizedAlways {
self.locationManager.startUpdatingLocation()
}
else {
print("it took the else route")

}

print("status is \(status)")
self.newMap.layer.cornerRadius = 8
self.newMap.clipsToBounds = true

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 0 {
locationManager.stopUpdatingLocation()
let location1 = locations.first as CLLocation!
let mapSpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let mapRegion = MKCoordinateRegion(center: location1.coordinate, span: mapSpan)
self.newMap.setRegion(mapRegion, animated: true)
}
}


func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

if status == CLAuthorizationStatus.Denied {
print("there will be limited functionaliy")
}
else if status == CLAuthorizationStatus.AuthorizedAlways {
self.locationManager.startUpdatingLocation()
}
else {
print("it took the else route")
}
}


}

我的 info.plist 有这个键:

  <key>NSLocationAlwaysUsageDescription</key>
<string>Get User Location</string>

最佳答案

错误消息告诉您必须做什么。你需要调用

self.locationManager.requestWhenInUseAuthorization() 

self.locationManager.requestAlwaysAuthorization()

在尝试启动位置管理器之前在您的代码中。除了将 NSLocationAlwaysUsageDescription 和/或 NSLocationWhenInUseUsageDescription 键添加到 info.plist 之外,还有该步骤。

有关更多信息,请参阅有关这些方法的文档。

(查看您发布的代码,您没有正确设置。您不能只调用 requestWhenInUseAuthorization 然后立即调用 startUpdatingLocation。)

正如文档所说:

When the current authorization status is kCLAuthorizationStatusNotDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services.

关键字是异步

您需要进行该调用,然后实现 locationManager:didChangeAuthorizationStatus: 方法。如果用户授予您的应用使用位置服务的权限,则会调用该方法 (didChangeAuthorizationStatus)。

您的代码应该更像这样:

override func awakeFromNib() 
{
locationManager.delegate = self
let status = locationManager.status
if (status == kCLAuthorizationStatusNotDetermined)
{
locationManager.requestWhenInUseAuthorization()
}
else if (status == kCLAuthorizationStatusAuthorized)
{
locationManager.startUpdatingLocation()
}
else
{
//Handle other error cases here.
}
newMap.layer.cornerRadius = 8
newMap.clipsToBounds = true
}

optional func locationManager(_ manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
locationManager.startUpdatingLocation()
}

关于ios - 使用 CLLocationManager 获取用户位置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878220/

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