Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode Swift,地理位置赢得';我不知道我在哪里_Swift_Xcode - Fatal编程技术网

Xcode Swift,地理位置赢得';我不知道我在哪里

Xcode Swift,地理位置赢得';我不知道我在哪里,swift,xcode,Swift,Xcode,任何人都明白为什么地理位置不会告诉我我在哪里,而且权限也不会改变。。。。。我很困惑。。。笔记本电脑位置正常,应用程序权限也已设置,用户的允许权限点击框也不会出现 (iv)上面解释了这一切,除非我在框中键入更多内容,否则它不会发布,但是有人能解释为什么这段代码不工作吗?或者它是否显示没有错误,我还检查了-DEBUG-locations…仍然没有 import UIKit import MapKit import CoreLocation class MapScreen: UIVi

任何人都明白为什么地理位置不会告诉我我在哪里,而且权限也不会改变。。。。。我很困惑。。。笔记本电脑位置正常,应用程序权限也已设置,用户的允许权限点击框也不会出现

(iv)上面解释了这一切,除非我在框中键入更多内容,否则它不会发布,但是有人能解释为什么这段代码不工作吗?或者它是否显示没有错误,我还检查了-DEBUG-locations…仍然没有

  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

您也应该在控制台中得到一个错误,所以也要检查一下