[英]iOS app doesn't ask for location permission
My Swift-iOS app is meant to show the user's location on a map. 我的Swift-iOS应用程序旨在显示用户在地图上的位置。 However, the XCode debug console tells me I need to ask permission to show the user's location.
但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置。 I think, I do that but the dialog never comes up.
我想,我这样做,但对话永远不会出现。
Here is the error message, and below the ViewController where I call CLLocationManager::requestWhenInUseAuthorization()
这是错误消息,在ViewController下面我调用
CLLocationManager::requestWhenInUseAuthorization()
Error: 错误:
2014-06-30 21:25:13.927 RowingTracker2[17642:1608253] Trying to start MapKit location updates without prompting for location authorization.
2014-06-30 21:25:13.927 RowingTracker2 [17642:1608253]尝试在不提示位置授权的情况下启动MapKit位置更新。 Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。
ViewController: 视图控制器:
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.
}
}
How do I request authorization to use the location? 如何申请授权使用该位置? You can find the complete project here .( Commit )
你可以在这里找到完整的项目。( 提交 )
Even making ViewController inherit from CLLocationManagerDelegate
and setting the delegate to self
as indicated here doesn't help. 甚至使视图控制器继承
CLLocationManagerDelegate
和委托设置为self
所示这里没有帮助。
As of iOS 8 you have to call one of the request... functions and add the appropriate entry to your Info.plist file, either NSLocationWhenInUseUsageDescription
or NSLocationAlwaysUsageDescription
. 从iOS 8开始,您必须调用其中一个请求...函数,并将相应的条目添加到Info.plist文件中,
NSLocationWhenInUseUsageDescription
或NSLocationAlwaysUsageDescription
。
For more information, see the reference here 有关更多信息,请参阅此处的参考
Make sure that 确保这一点
Debug > Location
. Debug > Location
下的模拟器中执行此操作。 Debug Area: 调试区域:
您需要使用requestWhenInUseAuthorization并且还需要在yourapp-Info.plist上创建一个名为NSLocationWhenInUseUsageDescription的值
I use NSLocationAlwaysUsageDescription
with the value as the text that will pop up when asking for permission, such as 我使用
NSLocationAlwaysUsageDescription
,其值为在请求权限时弹出的文本,例如
"I would like permission to spy on you 24/7"
“我希望得到许可,可以24/7全天候监视你”
I would also add NSLocationWhenInUseUsageDescription
with the value as the message. 我还将添加
NSLocationWhenInUseUsageDescription
,并将值作为消息。
As David Berry , Cayke Prudente and Levi Johnson mentioned, I just needed to add NSLocationAlwaysUsageDescription
to my Info.plist
file. 正如David Berry , Cayke Prudente和Levi Johnson所提到的,我只需要将
NSLocationAlwaysUsageDescription
添加到我的Info.plist
文件中。 To understand more WHY I needed this, I went for further documentation and I'm sharing here, as it can help others the same way they helped me. 要了解更多我为什么需要这个,我去了进一步的文档,我在这里分享,因为它可以帮助他人,就像他们帮助我一样。
The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app's Info.plist file, and the presence of that key is required when calling this method.
用户提示包含应用程序的Info.plist文件中NSLocationWhenInUseUsageDescription键的文本,并且在调用此方法时需要存在该键。
https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.