随着基于位置服务LBS和移动互联网的兴起,你的位置是越来越重要的一个信息,位置服务已经是当前的热门应用如微信,陌陌等社交应用的杀手锏。而在iOS开发中,苹果已经给我们提供了一个位置接口,CoreLocation,我们可以使用该接口方便的获得当前位置的经纬度信息。具体实现如下:
(1)新建基于Swift的iOS项目,在ViewController中导入CoreLocation接口:
import CoreLocation
(2)在ViewController类中实现如下:
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManager:CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if ios8(){
locationManager.requestAlwaysAuthorization()
}
locationManager.startUpdatingLocation()
}
func ios8()->Bool{
return UIDevice.currentDevice().systemVersi