iOS应用不会要求位置许可

问题描述 投票:9回答:4

我的Swift-iOS应用程序旨在显示用户在地图上的位置。但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置。我想,我这样做,但对话永远不会出现。

这是错误消息,在ViewController下面我调用CLLocationManager::requestWhenInUseAuthorization()

错误:

2014-06-30 21:25:13.927 RowingTracker2 [17642:1608253]尝试在不提示位置授权的情况下启动MapKit位置更新。必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。

视图控制器:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var mapview: MKMapView = nil
    var locationmgr : CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationmgr = CLLocationManager()
        locationmgr.requestWhenInUseAuthorization()
        mapview.showsUserLocation = true
    }

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

如何申请授权使用该位置?你可以找到完整的项目here。(Commit

Info

甚至使ViewController从CLLocationManagerDelegate继承并将委托设置为self,如here所示也无济于事。

ios swift mapkit core-location
4个回答
26
投票

从iOS 8开始,您必须调用其中一个请求...函数,并将相应的条目添加到Info.plist文件中,NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

有关更多信息,请参阅参考here

Update

确保这一点

  1. 地图以模拟位置为中心。
  2. 还要确保模拟某个位置。要么在XCode的调试区域(下面)中执行此操作(请参见图像),要么在Debug > Location下的模拟器中执行此操作。

调试区域:


8
投票

您需要使用requestWhenInUseAuthorization并且还需要在yourapp-Info.plist上创建一个名为NSLocationWhenInUseUsageDescription的值


2
投票

我使用NSLocationAlwaysUsageDescription作为在请求许可时弹出的文本,例如

“我希望得到许可,可以24/7全天候监视你”

我还会添加NSLocationWhenInUseUsageDescription作为消息的值。


0
投票

正如David Berry,Cayke Prudente和Levi Johnson所说,我只需要将NSLocationAlwaysUsageDescription添加到我的Info.plist文件中。要了解更多我为什么需要这个,我去了进一步的文档,我在这里分享,因为它可以帮助他人,就像他们帮助我一样。

用户提示包含应用程序的Info.plist文件中NSLocationWhenInUseUsageDescription键的文本,并且在调用此方法时需要存在该键。

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization

© www.soinside.com 2019 - 2024. All rights reserved.