Avatar ·

Como obter as coordenadas atuais do usuário em Swift (CLLocationManager)?

Olá a todos! Sou novo no desenvolvimento iOS e não consigo entender como obter a localização atual do usuário. Preciso obter a latitude e longitude para enviá-las ao servidor. Conectei o CoreLocation, adicionei as chaves no Info.plist (NSLocationWhenInUseUsageDescription), escrevi o código, mas as coordenadas sempre vêm nil ou nil. Aqui está meu código: ```swift import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() if let location = locationManager.location { print("Lat: \(location.coordinate.latitude)") print("Lon: \(location.coordinate.longitude)") } else { print("Location is nil") } } ```