self.locationManager.requestAlwaysAuthorization() is only available on iOS 8.0 or newer - Swift

989 Views Asked by At

I am developing a simple ios app in swift that gets the location of the user and shows the location. I need to set this app to target ios 7.0 and higher so iPhone 4 users can use this app. However, when i set the Deployment Target to 7.0 I get a build error saying:

/Users/toing_toing/dev/xyz/HomePageViewController.swift:35:30: 'requestAlwaysAuthorization()' is only available on iOS 8.0 or newer

It comes from this line:

self.locationManager.requestAlwaysAuthorization()

I need the app to access GPS all the time for the moment, but I cant find a replacement code for this that will fix the deployment target error. What can I do?

1

There are 1 best solutions below

3
Ali Riahipour On BEST ANSWER

Just use self.locationManager.startUpdatingLocation()

var authorizationStatus = CLLocationManager.authorizationStatus()
if (authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways {
  locationManager.startUpdatingLocation()
}else if self.locationManager.respondsToSelector(Selector("requestAlwaysAuthorization")) {
   self.locationManager.requestAlwaysAuthorization()
}else {
   locationManager.startUpdatingLocation()
}