应用程序的Info.plist必须包含一个NSLocationWhenInUseUsageDescription键,其中包含一个字符串值,向用户解释应用程序如何使用此数据

时间:2017-12-15 16:04:44

标签: swift maps ios11

我是IOS的新手。我正在尝试使用Maps来获取用户的当前位置。我关注的教程适用于IOS 10.

我经历过这篇文章并做了所有的事情,但它仍然没有用 Location Services not working in iOS 11

以下是我的代码

class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    var locationManager=CLLocationManager()
    // @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        locationManager.delegate=self
        locationManager.desiredAccuracy=kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0]
        let latitude=userLocation.coordinate.latitude
        let longitude=userLocation.coordinate.longitude
        let latDelta:CLLocationDegrees=0.05
        let lonDelta:CLLocationDegrees=0.05
        let span=MKCoordinateSpan(latitudeDelta:latDelta, longitudeDelta:lonDelta)
        let location=CLLocationCoordinate2D(latitude:latitude,longitude:longitude)
        let region=MKCoordinateRegion(center:location,span:span)
        self.mapView.setRegion(region,animated:true)

    }



}

我在info.plist文件中添加了以下内容

<key>NSLocationAlwaysUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

如果您使用的是模拟器,请确保选择了一个位置。 选择模拟器,在菜单栏中需要转到Debug - &gt;位置 - &gt;然后做自定义或预定义。

如果你已经这样做了,请尝试下面的内容。

我不会在info.plist中手动输入该文本,而是使用默认编辑器打开它。

单击蓝色突出显示区域中显示的加号按钮(您可以真正选择其中任何一个)这将添加一个新行。 enter image description here

其次,输入隐私 - 位置时间...选择适用于您的那个。 enter image description here

左侧填写完毕后,双击右侧并输入要向用户显示的文本。 enter image description here