仅在首次启动时出现 requestWhenInUseAuthorization 错误
requestWhenInUseAuthorization error only upon first launch
错误:
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 仅在首次启动时抛出错误。在我什至同意位置服务提示之前。在随后的发布中,它工作正常。是因为应用程序启动的第一个视图控制器已经使用了地图吗?解决方法可能不是直接跳转到地图,而是先显示其他一些 VC。还有其他解决方法吗?
是的,你是对的,mapView.showsUserLocation=true
需要访问你的当前位置,如果不同意 OS 显示的警报,这是不可能的。您可以实现 CLLocationManagerDelegate
的 didChangeAuthorizationStatus
方法,并在其中包含与地图相关的代码。试试下面的代码。
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
}
}
错误:
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 仅在首次启动时抛出错误。在我什至同意位置服务提示之前。在随后的发布中,它工作正常。是因为应用程序启动的第一个视图控制器已经使用了地图吗?解决方法可能不是直接跳转到地图,而是先显示其他一些 VC。还有其他解决方法吗?
是的,你是对的,mapView.showsUserLocation=true
需要访问你的当前位置,如果不同意 OS 显示的警报,这是不可能的。您可以实现 CLLocationManagerDelegate
的 didChangeAuthorizationStatus
方法,并在其中包含与地图相关的代码。试试下面的代码。
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
}
}