swift 学习笔记 2019-04-04

MACOS 系统定位服务
Privacy - Location Always Usage Description - 描述
Privacy - Location When In Use Usage Description - 描述

lazy var locationManager:CLLocationManager = {       
         let locationManager = CLLocationManager()
         locationManager.delegate = DMLocation.shared
         return locationManager
     }()

    func getSunCalc(_ block: @escaping FKBlock1){
                  if CLLocationManager.locationServicesEnabled() {
             switch CLLocationManager.authorizationStatus() {
             case .restricted:
                 block("您无权使用位置服务.")
             case .denied:
                 block("您拒绝使用位置服务的权限,请在“首选项”中启用它。")
             case .notDetermined:
                 locationManager.startUpdatingLocation()
             default:
                 block("您无权使用位置服务.")
             }
         }else{
         }
     }

extension DMLocation : CLLocationManagerDelegate{
     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
         switch status {
                      case CLAuthorizationStatus.notDetermined:
             print("LocationManager授权状态未确定。 (用户尚未选择)")
         case CLAuthorizationStatus.authorized:
             print("我们现在已获得locationServices的授权")
             locationManager.startUpdatingLocation()
//        case CLAuthorizationStatus.authorizedAlways:
         case CLAuthorizationStatus.denied:
             print("LocationManager:我们现在被明确拒绝了")
 //            requestWhenInUseAuthorization
             locationManager.stopUpdatingLocation()
         case CLAuthorizationStatus.restricted:
             print("LocationManager我们现在受限制了! (未经授权 - 可能是由于家长控制......)")
             locationManager.stopUpdatingLocation()
         default: break
         }    }
         func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let lt = locations.last {
            loaction = lt
            locationManager.stopUpdatingLocation()
            block(lt)
         }
     }
          func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
         block("无法获取定位地址" + error.localizedDescription)
     }
     」

你可能感兴趣的:(swift 学习笔记 2019-04-04)