The task is to get the user data (City where it is located) to print it out using print) and send it to the server, in all the examples that I found we use map view, and I don’t need it in the project, only data is needed. I understand that I need to use the CLLocation Manager, but I don’t understand how to implement it correctly yet.
1 answer
Basic. If you want to receive user coordinates, add the NSLocationWhenInUseUsageDescription key to NSLocationWhenInUseUsageDescription :
Then in your class (example for UIViewController):
import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } // Получаем координаты func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let coordinates = locations.last?.coordinate else { return } // Смотрим на координаты print(coordinates) } // Обрабатываете ошибки func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error.localizedDescription) } } Further with the received coordinates you do anything.
|
