美文网首页
IOS获取前台定位方法

IOS获取前台定位方法

作者: Yumazhiyao | 来源:发表于2016-08-03 14:54 被阅读27次
    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

    相关文章

      网友评论

          本文标题:IOS获取前台定位方法

          本文链接:https://www.haomeiwen.com/subject/qabqsttx.html