Search code examples
iosswiftxcodeswift4

when i run my app it doesn't register the user location but when I change the location in the debugger it starts registering it normally


when I first run the app it doesn't get the user location automatically I have to manually add the location in debugger so then it registers it.

    func viewDidLoad() {
    super.viewDidLoad()
    //permissions, in ViewDidLoad()
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        locationManager.pausesLocationUpdatesAutomatically = false
    }



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let _ : CLLocationCoordinate2D = manager.location?.coordinate else{
        return
    }

    geoFire!.setLocation(locations.last!, forKey: Auth.auth().currentUser!.uid)

    InRange(location: locations.last!)

    for ids in keysInCircle{
        fetchInformation(idlabel: ids)
    }

}

Solution

  • It's probably because you didn't start updating the location before setting any other property of location. You should call 'startUpdatingLocation()' function first.

    Try changing your code as given below.

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.pausesLocationUpdatesAutomatically = false
    
    }