H.N. H.N. - 15 days ago 5
Swift Question

Stop updating location when view (map) is not showing

I have an app with a map loaded immediately after the app starts.
Now I have some views in the app.
WHat I'd like to do is, whene I leave my mapview, into another view, I'd like the map to stop updating self location and than, only start updating again self location, when I come back to that map view.
(self location = user current location).

Anyone can tell me how to do that please?

Thanks.

Rob Rob
Answer

You can use locationM.stopUpdatingLocation()

Start updating location

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    locationM.startUpdatingLocation()


}

And Stop

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    locationM.stopUpdatingLocation()
}
Comments