问题描述
我已在info.plist中添加了三个权限
和弹出窗口带有三个选项。..
1)如果我单击"allow while using app"
,则需要使用当前位置地址自动填充文本字段,但是
- 如果我点击
"allow once"
,则不需要使用当前位置自动填充文本字段
在我的代码中,一次允许和一次允许使用条件文本字段都自动填充当前位置地址:
class MapViewController: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var latitude: String?
var logitude: String?
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var text3: UITextField!
let annotation = MKPointAnnotation()
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
guard let _: CLLocationCoordinate2D = manager.location?.coordinate else { return }
let userLocation :CLLocation = locations[0] as CLLocation
latitude = "\(userLocation.coordinate.latitude)"
logitude = "\(userLocation.coordinate.longitude)"
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(userLocation) { (placemarks,error) in
if (error != nil){
print("error in reverseGeocode")
}
let placemark = placemarks! as [CLPlacemark]
if placemark.count>0{
let placemark = placemarks![0]
let placemarkDictonary: NSDictionary=placemark.addressDictionary as! NSDictionary
self.text1.text=placemarkDictonary["ZIP"] as? String
self.text2.text=placemarkDictonary["City"] as? String
self.text3.text=placemarkDictonary["Street"] as? String
}
}
let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center,span: MKCoordinateSpan(latitudeDelta: 0.1,longitudeDelta: 0.1))
mapView.setRegion(region,animated: true)
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude,userLocation.coordinate.longitude);
myAnnotation.title = "Current location"
mapView.addAnnotation(myAnnotation)
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .denied:
print("User hates you!!")
case .authorizedWhenInUse:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
self.navigationController?.pushViewController(viewController,animated: true);
case .authorizedAlways:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
self.navigationController?.pushViewController(viewController,animated: true);
}
}
}
实际上,当我单击"allow once"
时,我仅需要当前位置坐标,而无需在文本字段中自动填充地址。该怎么做?
请提供代码帮助。
解决方法
When In Use
和Allow Once
具有相同的权限,但有效期限不同。在位置权限提示上轻按Allow Once
,将为应用程序的当前会话启用When In Use
权限,即。直到用户强行杀死该应用程序或该应用程序长时间在后台停留。如果用户点击了Allow Once
权限,您将获得所有位置更新,就像他授予了When In Use
权限一样。但是,如果用户杀死该应用程序并重新启动它,系统将再次显示位置许可提示。
因此,通过检查locationManager(_ manager: CLLocationManager,didChangeAuthorization
返回的状态将无法实现您要执行的操作,因为这两个权限都将其状态显示为authorizedWhenInUse
。您无法区分它们。