【问题标题】:The app's Info.plist must contain an NSLocationWhenInUseUsageDescription key with a string value explaining to the user how the app uses this data应用的 Info.plist 必须包含一个 NSLocationWhenInUseUsageDescription 键和一个字符串值,向用户解释应用如何使用这些数据
【发布时间】:2017-12-15 16:04:43
【问题描述】:

我是 IOS 新手。我正在尝试使用地图来获取用户的当前位置。我正在关注的教程适用于 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>

任何帮助将不胜感激。

【问题讨论】:

  • 定义“不起作用”。这不是一个有用的描述。您发布的代码的确切问题是什么?
  • 我无法获取用户的当前位置。它甚至没有给我权限对话框?
  • 你是在模拟器还是真机上工作?如果模拟器(并提供 GPS 在那里工作),请尝试重置它,然后重新安装它。如果是设备,请尝试删除该应用并重新安装。
  • @dfd GPS 确实可以在模拟器中工作,但是在菜单栏中,您需要转到 Debug -> Location -> 然后进行自定义或预定义。
  • 一个 ios 项目有几个 info.plist 文件,确保你编辑的是正确的!

标签: swift maps ios11


【解决方案1】:

如果您使用的是模拟器,请确保您选择了一个位置。 选择模拟器,在菜单栏中你需要去Debug -> Location -> 然后要么做一个自定义的要么做一个预定义的。

如果你已经这样做了,试试下面的。

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

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

其次,输入隐私 - 位置... 选择适用于您的选项。

填写完左侧后,双击右侧并输入要显示给用户的文本。

【讨论】:

    【解决方案2】:

    info.plst中应该有3个描述

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 2017-03-04
      • 1970-01-01
      相关资源
      最近更新 更多