Xcode Swift,地理位置赢得';我不知道我在哪里
任何人都明白为什么地理位置不会告诉我我在哪里,而且权限也不会改变。。。。。我很困惑。。。笔记本电脑位置正常,应用程序权限也已设置,用户的允许权限点击框也不会出现 (iv)上面解释了这一切,除非我在框中键入更多内容,否则它不会发布,但是有人能解释为什么这段代码不工作吗?或者它是否显示没有错误,我还检查了-DEBUG-locations…仍然没有Xcode Swift,地理位置赢得';我不知道我在哪里,swift,xcode,Swift,Xcode,任何人都明白为什么地理位置不会告诉我我在哪里,而且权限也不会改变。。。。。我很困惑。。。笔记本电脑位置正常,应用程序权限也已设置,用户的允许权限点击框也不会出现 (iv)上面解释了这一切,除非我在框中键入更多内容,否则它不会发布,但是有人能解释为什么这段代码不工作吗?或者它是否显示没有错误,我还检查了-DEBUG-locations…仍然没有 import UIKit import MapKit import CoreLocation class MapScreen: UIVi
import UIKit
import MapKit
import CoreLocation
class MapScreen: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
} else {
// Show alert letting the user know they have to turn this on.
}
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
centerViewOnUserLocation()
locationManager.startUpdatingLocation()
break
case .denied:
// Show alert instructing them how to turn on permissions
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// Show an alert letting them know what's up
break
case .authorizedAlways:
break
}
}
}
extension MapScreen: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
}
您是否已将权限字符串添加到info.plist 你需要为这些添加解释,否则权限提示将不会出现,并且不会向你的应用发送任何位置更新
nslocationalwayusagedescription
和nslocationwhenusagedescription
您也应该在控制台中得到一个错误,所以也要检查一下