ios 位置权限检查和授权
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38726873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Location permission check and authorization
提问by Steve
I want my program to display the users location on the map. It was working at first then randomly stopped so i added the code below to try to fix it but I'm having problems. Thanks in advance!
我希望我的程序在地图上显示用户位置。它开始工作然后随机停止,所以我添加了下面的代码来尝试修复它,但我遇到了问题。提前致谢!
override func viewDidLoad()
{
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.locationServicesEnabled()
{
let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined
{
locationManager.requestAlwaysAuthorization()
}
} else {
print("locationServices disenabled")
}
locationManager.startUpdatingLocation()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
mapView.delegate = self
centerMapOnLocation(initialLocation)
addBoundry()
}
回答by Ketan Parmar
You required to call,
你需要打电话,
locationManager.requestAlwaysAuthorization()
as well
还有
locationManager.requestWhenInUseAuthorization()
to use location smoothly in foreground also!!!
也可以在前台顺利使用位置!!!
and you should not need two instance to call startupdatinglocation
. keep one. you should use instance or global variable instead of local to get location throughout the scope.
并且您不应该需要两个实例来调用startupdatinglocation
. 留一个。您应该使用实例或全局变量而不是局部变量来获取整个范围内的位置。
Update :
更新 :
You have to set two keys in info.plist
like, NSLocationAlwaysUsageDescription
and NSLocationWhenInUseUsageDescription
with it's usage description.
你必须在info.plist
like中设置两个键,NSLocationAlwaysUsageDescription
以及NSLocationWhenInUseUsageDescription
它的用法说明。
回答by Chris Ashton
I was having the same issue, even with NSLocationAlwaysUsageDescription
and NSLocationWhenInUseUsageDescription
set in info.plist. My debugger told me to also set NSLocationAlwaysAndWhenInUseUsageDescription
...so I did it. It worked!
我遇到了同样的问题,即使在 info.plist 中设置NSLocationAlwaysUsageDescription
和NSLocationWhenInUseUsageDescription
设置。我的调试器告诉我也设置NSLocationAlwaysAndWhenInUseUsageDescription
......所以我做到了。有效!
回答by Blue1905
If you want the "WhenInUse" notification you should only add "Privacy - Location When In Use Usage Description" in your info.plist but if you want the "Always" access your info.plist should look like this info.plist screenshot:
如果你想要“WhenInUse”通知,你应该只在你的 info.plist 中添加“Privacy - Location When In Use Usage Description”,但如果你想要“Always”访问你的 info.plist 应该看起来像这个 info.plist 截图:
You don't have to add any request authorization like requestAlwaysAuthorization or requestWhenInUseAuthorization
您不必添加任何请求授权,如 requestAlwaysAuthorization 或 requestWhenInUseAuthorization