First add the CoreLocation.framework
to the project.
Then use the following code to get the location:
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager! = nil
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!,
didUpdateToLocation newLocation: CLLocation!,
fromLocation oldLocation: CLLocation!) {
let longitude = newLocation.coordinate.longitude
let latitude = newLocation.coordinate.latitude
longitudeLabel.text = longitude.description
latitudeLabel.text = latitude.description
//If you just want to get once, use this code
//to stop the updating location.
locationManager.stopUpdatingLocation()
}
}