CLLocation Manager in Swift to get Location of User

You are missing two things. First, you have to ask for permission using requestAlwaysAuthorization or requestWhenInUseAuthorization(). So your viewDidLoad() should be like this: var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } Second, … Read more

How to find your current location with CoreLocation

You can find your location using CoreLocation like this: import CoreLocation: #import <CoreLocation/CoreLocation.h> Declare CLLocationManager: CLLocationManager *locationManager; Initialize the locationManager in viewDidLoad and create a function that can return the current location as an NSString: – (NSString *)deviceLocation { return [NSString stringWithFormat:@”latitude: %f longitude: %f”, locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; } – (void)viewDidLoad { locationManager = [[CLLocationManager alloc] … Read more

How to get Current Location using SwiftUI, without ViewControllers?

You could create a StateObject of your LocationManager by implementing the ObservableObject protocol. With the @Published attribute you can create a publisher object which notify the observers (your view, in this case) when something changes inside that object. That’s why in my LocationManager I added the @Published attribute to those var: locationStatus: CLAuthorizationStatus? it contains … Read more