Kashif
Kashif

Reputation: 4632

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:

locationManager?.stopUpdatingLocation()

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.

Upvotes: 0

Views: 1348

Answers (1)

Kashif
Kashif

Reputation: 4632

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:

appDelegate.locationManager!.requestAlwaysAuthorization()

Hope this helps others.

Upvotes: 4

Related Questions