0

I'm working with swift 2.0 and on a project that has the deployment target as 7.0. And when I use

location.requestAlwaysAuthorization()

error is thrown . And I know this particular method isn't there for ios 7. my Question is .. If i add

@available(ios 8.0 , *)

Would it work on devices with ios 7.0 or this particular function would be processed only if the device is >= ios 8.0 ? Kindly help.

ebby94
  • 3,131
  • 2
  • 23
  • 31
AnxiousMan
  • 578
  • 2
  • 8
  • 25
  • http://stackoverflow.com/questions/24167791/what-is-the-swift-equivalent-of-respondstoselector ? – Larme Mar 16 '16 at 10:02
  • @Larme, I have gone through that answer , but could not understand clearly . So can u please make it a little clear ? – AnxiousMan Mar 16 '16 at 10:05

2 Answers2

1

The particular function will be processed only if the device is >= iOS 8.0.

if #available(iOS 8, *)
{
    location.requestAlwaysAuthorization()
} 
else
{
    locationManager.startUpdatingLocation()
}

Using this will call requestAlwaysAuthorization() for devices running on iOS 8+, otherwise it will call startUpdatingLocation().

ebby94
  • 3,131
  • 2
  • 23
  • 31
  • I'm checking this as well @ebby94 if (location.respondsToSelector(Selector(location.requestWhenInUseAuthorization()))‌​) . how do i check that ? – AnxiousMan Mar 16 '16 at 10:17
  • You can't use `requestWhenInUseAuthorization()` in devices running on iOS 7 and lower. Even if you use the `respondsToSelector` method, Xcode will throw an error if your deployment target is lower than iOS 8 and will ask you to fix it with `if #available(iOS 8, *)`. – ebby94 Mar 16 '16 at 10:51
0

On iOS7, CLLocationManager requests location authorisation to the user when calling startUpdatingLocation. Usually, on a iOS7 compatible application, you end up writing

if #available(iOS 8.0, *) {
  locationManager.requestAlwaysAuthorization()
}
locationManager.startUpdatingLocation()

This way, location authorisation is requested either by requestAlwaysAuthorization or startUpdatingLocation depending on the OS.

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • I'm checking this as well @tomahh if (location.respondsToSelector(Selector(location.requestWhenInUseAuthorization()))) . how do i check that ? – AnxiousMan Mar 16 '16 at 10:14
  • As of Swift 2, the recommended way to check for API availability is through #available() statement. See [this apple blog post](https://developer.apple.com/swift/blog/?id=29) (under the availability title) for details. – tomahh Mar 16 '16 at 10:17