IOS获取前台定位方法

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容