Petr Mánek
Petr Mánek

Reputation: 1066

How to implement CLLocationManager.requestLocation() on iOS 8 or older?

If you have read the new iOS 9 docs, you may have noticed a new method which causes the location manager to power up radios for a brief amount of time, get a fix on your location and once desired accuracy (or timeout) is reached, turn it all back off, handing the pinpointed location over to the delegate.

The method is called CLLocationManager.requestLocation() and is available in the iOS 9 SDK. Alas, I'm currently working on an app targeting iOS 8 and would still very much like to make use of this method. I also wouldn't like to reimplement it all myself.

So here's my question: Is there any open-source library for iOS implementing this kind of one-time location retrieval?

Upvotes: 1

Views: 1445

Answers (2)

baydi
baydi

Reputation: 1003

You need to perform two steps first

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

Then

var locationManager = CLLocationManager()
    locationManager.delegate = self
    // locationManager.locationServicesEnabled
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    let Device = UIDevice.currentDevice()

    let iosVersion = NSString(string: Device.systemVersion).doubleValue

    let iOS8 = iosVersion >= 8

    if iOS8{

        locationManager.requestWhenInUseAuthorization()
    }
else{
locationManager.requestAlwaysAuthorization()
}

locationManager.startUpdatingLocation()

This will Help.Thanksyou

Upvotes: 2

Petr Mánek
Petr Mánek

Reputation: 1066

As Arslan Asim pointed out, a good solution would be to use INTULocationManager.

From the docs:

manager.requestLocationWithDesiredAccuracy(.Block, timeout: 10.0) {
    (currentLocation, achievedAccuracy, status) in
    // Examine status to see if the request timed out.
    // If not, currentLocation stores the found location.
}

Upvotes: 0

Related Questions