迅速处理位置权限 [英] handling location permissions instantaneously in swift
问题描述
我正在尝试实现基本地图视图,并将用户的当前位置作为注释添加到地图中。我已经将requestwheninuse键添加到我的info.plist并导入了coreLocation。
I am trying to implement a basic map view and add a user's current location to the map as an annotation. I have added the requestwheninuse key to my info.plist and imported coreLocation.
在我的视图控制器的加载方法中,我有以下内容:
In my view controller's did load method, I have the following:
locManager.requestWhenInUseAuthorization()
var currentLocation : CLLocation
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
currentLocation = locManager.location
println("currentLocation is \(currentLocation)")
}
else{
println("not getting location")
// a default pin
}
我得到了提示重新。检索位置的权限。随着这种情况的发生,我得到的是我的打印说没有获得位置,显然是因为这在用户有机会点击OK之前运行。如果我完成应用程序并返回,我可以检索位置并将其添加到地图中。但是,我希望当用户第一次点击OK然后能够抓住当前位置并将其添加到地图然后。我怎样才能做到这一点?我有以下添加引脚的方法:
I am getting the prompt re. permission to retrieve location. As this is happening I am getting my print saying not getting location, obviously because this runs before the user gets a chance to tap OK. If I elave the app and come back in I can retrieve the location and add it to the map. However, I want when the user taps OK the first time to be able to then grab the current location and add it to the map there and then. How can I achieve this? I have the following method for adding a pin:
func addPin(location2D: CLLocationCoordinate2D){
self.mapView.delegate = self
var newPoint = MKPointAnnotation()
newPoint.coordinate = location2D
self.mapView.addAnnotation(newPoint)
}
推荐答案
为此,您需要实现方法 didChangeAuthorizationStatus
用于您的位置管理器委托,该委托在 CLLocationManager
初始化后不久被调用。
In order to do that, you need to implement the methoddidChangeAuthorizationStatus
for your location manager delegate which is called shortly after CLLocationManager
is initialized.
首先,在文件顶部不要忘记添加: import CoreLocation
First, at the top of the file don't forget to add : import CoreLocation
要做到这一点,在你的班级里正在使用该位置,添加委托协议。然后在 viewDidLoad
方法(或 applicationDidFinishLaunching
,如果你在 AppDelegate
)初始化您的位置管理器并将其委托
属性设置为 self
:
To do that, in your class where you are using the location, add the delegate protocol. Then in the viewDidLoad
method (or applicationDidFinishLaunching
if you are in the AppDelegate
) initialize your location manager and set its delegate
property to self
:
class myCoolClass: CLLocationManagerDelegate {
var locManager: CLLocationManager!
override func viewDidLoad() {
locManager = CLLocationManager()
locManager.delegate = self
}
}
最后,在您之前声明的类的主体中实现locationManager(_ didChangeAuthorizationStatus _)方法,此方法将在授权状态已更改,因此只要用户单击该按钮即可。您可以这样实现:
Finally, implement the locationManager(_ didChangeAuthorizationStatus _) method in the body of your class that you declared previously, this method will be called when the status of the authorization is changed, so as soon as your user clicked the button. You can implement it like this:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined:
// If status has not yet been determied, ask for authorization
manager.requestWhenInUseAuthorization()
break
case .AuthorizedWhenInUse:
// If authorized when in use
manager.startUpdatingLocation()
break
case .AuthorizedAlways:
// If always authorized
manager.startUpdatingLocation()
break
case .Restricted:
// If restricted by e.g. parental controls. User can't enable Location Services
break
case .Denied:
// If user denied your app access to Location Services, but can grant access from Settings.app
break
default:
break
}
}
Swift 4 - 新枚举语法
对于Swift 4,只需将每个枚举大小写的第一个字母切换为小写(.notDetermined,.authorizedWhenInUse ,. authorizedAlways,.restricted和.denied)
For Swift 4, just switch the first letter of each enum case to lowercase (.notDetermined, .authorizedWhenInUse, .authorizedAlways, .restricted and .denied)
这样你可以处理每一个案例,用户只是给予了许可或撤销了它。
That way you can handle each and every case, wether the user just gave its permission or revoked it.
这篇关于迅速处理位置权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!