Posts

Post not yet marked as solved
28 Replies
please make sure to add the following to info.plist Privacy - Location Always and When In Use Usage Description Privacy - Location When In Use Usage Description Privacy - Location Temporary Usage Description Dictionary -> wantAccurateLocation checkout this post medium.com/better-programming/handling-location-permissions-in-ios-14-2cdd411d3cca import UIKit import CoreLocation class ActivitiesMapVC: UIViewController { 		private let locationManager = CLLocationManager() 		var locationStatus = "..." 	 		override func viewDidLoad() { 				super.viewDidLoad() 				self.locationConfig() 		} 	 		func locationConfig(){ 				self.locationManager.delegate = self 				self.locationManager.requestWhenInUseAuthorization() 				self.locationManager.requestAlwaysAuthorization() 		} 		func checkLocationAccuracyAllowed() { 				switch locationManager.accuracyAuthorization { 						case .reducedAccuracy: 								locationStatus = "approximate location" 						case .fullAccuracy: 								locationStatus = "accurate location" 						default: 								locationStatus = "unknown type" 				} 				locationManager.startUpdatingLocation() 		} 		func requestLocationAuth() { 				locationManager.requestAlwaysAuthorization() 				locationManager.desiredAccuracy = kCLLocationAccuracyReduced 				let url = URLs() 				switch locationManager.authorizationStatus { 						case .denied: // Setting option: Never 								let alert = UIAlertController(title: "Location Denied", message: "Please enable location", preferredStyle: .alert) 								alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: { [weak self] _ in 										self?.dismiss(animated: true, completion: nil) 								})) 								alert.addAction(UIAlertAction(title: "Go to Settings", style: .default, handler: { _ in 										url.openURL(urlString: URLs.locationAppURL) 								})) 								alert.firstActiveTemplateAlert() 								self.present(alert, animated: true, completion: nil) 						case .notDetermined: break 						case .authorizedWhenInUse: 								// While using the app 								locationManager.requestAlwaysAuthorization() 								locationManager.requestLocation() 						case .authorizedAlways: 								// Always allow 								locationManager.requestAlwaysAuthorization() 								checkLocationAccuracyAllowed() 						case .restricted: // Restricted by parental control 								self.dismiss(animated: true, completion: nil) 						default: 								break 				} 		} } extension ActivitiesMapVC : CLLocationManagerDelegate { 		func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 				guard let location = locations.last else { return } 				print(location.coordinate.latitude,location.coordinate.longitude) 		} 		func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { 				let status = manager.authorizationStatus 				let accuracyStatus = manager.accuracyAuthorization 				if(status == .authorizedWhenInUse || status == .authorizedAlways){ 						if accuracyStatus == CLAccuracyAuthorization.reducedAccuracy{ 								locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "wantAccurateLocation", completion: { [self]	error in 										if locationManager.accuracyAuthorization == .fullAccuracy{ 												locationStatus = "Full Accuracy Location Access Granted Temporarily" 										}else{ 												locationStatus = "Approx Location As User Denied Accurate Location Access" 										} 										locationManager.startUpdatingLocation() 								}) 						} 				}else{ 						requestLocationAuth() 				} 		} 	 }