6

I use code from here:

import UIKit 
import CoreLocation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    initLocationManager();
    return true
}

// Location Manager helper stuff
func initLocationManager() {
    seenError = false
    locationFixAchieved = false
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.locationServicesEnabled
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestAlwaysAuthorization()
}

// Location Manager Delegate stuff

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationManager.stopUpdatingLocation()
    if (error) {
        if (seenError == false) {
            seenError = true
           print(error)
        }
    }
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as CLLocation
        var coord = locationObj.coordinate

        println(coord.latitude)
        println(coord.longitude)
    }
}

func locationManager(manager: CLLocationManager!,
    didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        var shouldIAllow = false

        switch status {
        case CLAuthorizationStatus.Restricted:
            locationStatus = "Restricted Access to location"
        case CLAuthorizationStatus.Denied:
            locationStatus = "User denied access to location"
        case CLAuthorizationStatus.NotDetermined:
            locationStatus = "Status not determined"
        default:
            locationStatus = "Allowed to location Access"
            shouldIAllow = true
        }
        NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
        if (shouldIAllow == true) {
            NSLog("Location to Allowed")
            // Start location services
            locationManager.startUpdatingLocation()
        } else {
            NSLog("Denied access: \(locationStatus)")
        }
}
}

But I have an error: 'locationServicesEnabled' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift.

Anyone know, how to fix that?

Thanks!

Community
  • 1
  • 1
Leon
  • 6,316
  • 19
  • 62
  • 97

1 Answers1

9

The locationServicesEnabled property on an instance of CLLocationManager is deprecated as of iOS 4.0, but the class method is not.

So instead of:

locationManager.locationServicesEnabled

You should simply use the following instead:

CLLocationManager.locationServicesEnabled()
smileyborg
  • 30,197
  • 11
  • 60
  • 73
  • instance method is deprecated since iOS 4.0 – bllakjakk Oct 11 '14 at 20:59
  • @smileyborg thanks! Sorry, I forgot about error with ```AnyObject[]!```. Must I use ```[AnyObject]!```? – Leon Oct 11 '14 at 21:00
  • @tim The latest Swift syntax for that delegate method is: `locationManager(_ manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)` – smileyborg Oct 11 '14 at 21:02
  • @smileyborg I have an errors: App -[CLLocationManager requestAlwaysAuthorization]: unrecognized selector sent to instance 0x7b6d1550 App *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocationManager requestAlwaysAuthorization]: unrecognized selector sent to instance 0x7b6d1550' – Leon Oct 11 '14 at 21:10
  • @tim Those are new methods only available in iOS 8.0 or higher. If you are running on iOS 7 or earlier you will get those crashes. If you are trying to support both you may want to look at using a library like [INTULocationManager](https://github.com/intuit/LocationManager) which handles all the work for you. – smileyborg Oct 11 '14 at 21:12
  • Otherwise the option is to use these method calls like below: if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestAlwaysAuthorization]; } – bllakjakk Oct 11 '14 at 21:18
  • @bllakjakk Thanks for your help. I always have message - **Denied access: Status not determined**. Does it normal? I use Debug/Simulate location – Leon Oct 11 '14 at 21:25
  • @tim Did you get the solution for Denied access: Status not determined? – Anurag Sharma Mar 09 '17 at 06:47