import UIKit
//1,导入CoreLocation
import CoreLocation
class ViewController: UIViewController {
//2,懒加载CLLocationManager
lazy var locationM : CLLocationManager = {
let locationM = CLLocationManager()
//3,设置代理
locationM.delegate = self
return locationM
}()
override func viewDidLoad() {
super.viewDidLoad()
//5,发送请求,调用该方法,只在App进入前台时候进行定位,并且需要在info.plist中加上NSLocationWhenInUseUsageDescription这个键,值随便填
(见下图)
locationM.requestWhenInUseAuthorization()
//6,启动定位
locationM.startUpdatingLocation()
}
}
//遵守协议
extension ViewController : CLLocationManagerDelegate {
//4,实现代理方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
print("-----")
}
}
Snip20160803_3.png
网友评论