Kashif Kashif - 6 months ago 3
Swift Question

Accessing locaitonManager from both AppDelegate and ViewController

In my app delegate class, I am declaring locaitonManager:

var locationManager: CLLocationManager?

Later in my app inside one of the view controller classes, I need to stop location updates:


But I get error:

Value of type CLLocation Manager.... has no member ....

because view controller cannot access the location manager declared inside app delegate class. What is my simple way out? I have to have locationManager access in app delegate.

Note: Strangely, my
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

is inside the view controller too, and it seems to be working fine.


To answer my own question for future reference:

In ViewController's class:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

Then later in the code I am able to refer to the locationManager declared in App Delegate like this:


Hope this helps others.