swift自定义定位工具类

2020-05-03  本文已影响0人  越天高
import UIKit
import CoreLocation

typealias LocationResultBlock = (CLLocation? , String?)->()
class LocationTool: NSObject
{
    var isOnce : Bool = false
    var resultBlock : LocationResultBlock?
    
    
    static let shareTool : LocationTool = LocationTool()
    
    //创建locationManager对象
   
    lazy var locationManager : CLLocationManager =
    {
        let locationM = CLLocationManager()
        locationM.delegate = self
        if #available(iOS 8.0, *)
        {
            //请求t前后台定位
            
            //请求前台定位
            //1. 取得info.plist文件的内容
            guard let infoDic = Bundle.main.infoDictionary else {
                return locationM
            }
            //2. 取的前台定位授权的key
            let whenInUse = infoDic["NSLocationWhenInUseUsageDescription"]
            //3. 取得后台定位授权的key 值
            let always = infoDic["NSLocationAlwaysUsageDescription"]
            
            // 4. 判断: 如果两个都有, 请求权限比较高的哪一个
            //  如果只有某一个, 那就请求对应的授权
            //  如果两个都没有, 给其他开发者提醒
            if always != nil {
                locationM.requestAlwaysAuthorization()
            }else if whenInUse != nil
            {
                locationM.requestWhenInUseAuthorization()
                // 1. 判断后台模式有没有勾选location updates
                // 2. 判断当前版本时候是ios9.0, 因为只有在ios9.0以后, 才需要额外的设置一个属性为true
                let backMode = infoDic["UIBackgroundModes"]
                if backMode != nil
                {
                    let backModels = backMode as! [String]
                    
                    if backModels.contains("location")
                    {
                        if #available(iOS 9.0, *)
                        {
                            locationM.allowsBackgroundLocationUpdates = true
                                
                        }
                    }
                }
                
            }else
            {
                print("在ios8.0以后,想要使用用户位置,难道不知道要主动请求授权吗? 你应该, 在info.plist 配置NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription")
            }

            
        }
        
        return locationM
    }()
    
    
    ///得到当前位置
    func getCurrentLocation(isOnce : Bool ,resultBlock : @escaping  LocationResultBlock)
    {
        self.isOnce = isOnce
        
        //1. 记录block
        self.resultBlock = resultBlock
        
        //2. 判断设备是否可以定位
        if CLLocationManager.locationServicesEnabled()
        {
            if isOnce //只定位一次
            {
                if #available(iOS 9.0, *) {
                    locationManager.requestLocation()
                } else {
                   
                }
            }else
            {
                locationManager.startUpdatingLocation()

            }
        }else
        {
            if self.resultBlock != nil
            {
                self.resultBlock!(nil, "没有打开定位权限")
            }
        }
    }
    
    

}
 //MARK:- lcoation的代理 -
extension LocationTool : CLLocationManagerDelegate
{
    //h超时
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        locationManager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        guard let newLocation = locations.last else
        {
            if self.resultBlock != nil {
                self.resultBlock!(nil, "没有获得定位信息")
            }
             return
        }
        resultBlock!(newLocation , nil)
        if isOnce {
            locationManager.stopUpdatingLocation()
        }
    }
    //用户定位权限状态
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
    {
        switch status
        {
        case .notDetermined:
            print("用户未决定")
        case .restricted:
            print("功能受限")
        case .authorizedWhenInUse:
            print("前台定位")
        case .authorizedAlways:
            print("后台定位")
        case .denied:
            print("用户拒绝 请手动打开")
        default:
            print("none")
        }
            
    }
}

上一篇下一篇

猜你喜欢

热点阅读