How to access CLLocationManager from Swift?

advertisements

Get User's Current Location / Coordinates has something that appears to be what I want:

you should do those steps:

  1. add CoreLocation.framework to BuildPhases -> Link Binary With Libraries
  2. import CoreLocation to your class - probably ViewController.swift
  3. add CLLocationManagerDelegate to your class decleration
  4. Add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to plist
  5. init location manager:

    locationManager = CLLocationManager()
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
    
    
  6. get User Location By:

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var userLocation:CLLocation = locations[0] as! CLLocation
        let long = userLocation.coordinate.longitude;
        let lat = userLocation.coordinate.latitude;
        //Do What ever you want with it
    }
    
    

I've poked around in Xcode, but I don't see where BuildPhases -> Link Binary with Libraries, or anything that follows.

If this is the approach I want, where in XCode do I add CoreLocation.framework to Link Binary with Libraries? If this is not the approach I want, how do I take advantage of Core Location?

Thanks,


You no longer have to manually link system frameworks (such as CoreLocation.framework) to your project. Just do a import CoreLocation at the top of the Swift file, and the framework will be seamlessly added for you (assuming you have the "Link Frameworks Automatically" setting to "Yes", which it is by default).

If you want to link to it manually, just go to "Build Phases" in your target settings: