// // SystemAuthorityFireBoltt.swift // FunDoHealth // // Created by ecell on 2018/5/5. // Copyright © 2020年 ecell. All rights reserved. // import Foundation import CoreLocation import Photos fileprivate enum AuthorityType { case location case photos case camera } final class SystemAuthorityFireBoltt: NSObject { /// 单例 static let shared = SystemAuthorityFireBoltt() private override init() {} // 结果回调 typealias resultBlock = (Bool) -> () var resultAAA: resultBlock? // MARK:- 获取定位权限 private let locationManager = CLLocationManager() func getLocationAuthority(result: @escaping resultBlock) { // 判断权限状态 switch CLLocationManager.authorizationStatus() { case .notDetermined: // 首次选择,获取定位 locationManager.requestAlwaysAuthorization() locationManager.requestWhenInUseAuthorization() // 监听点击 locationManager.delegate = self resultAAA = result case .restricted, .denied: // 提示用户开启权限 alertSetAuthority(type: .location) result(false) case .authorizedAlways, .authorizedWhenInUse: // 已开启权限 result(true) default: break } } // MARK:- 获取相册权限 func getPhotoAuthority(result: @escaping resultBlock) { let status = PHPhotoLibrary.authorizationStatus() // 已获取成功 if status == .authorized { result(true) return } // 未获取权限 if status == .notDetermined { PHPhotoLibrary.requestAuthorization { [weak self] newstatus in if newstatus == .authorized { result(true) return } // 提示用户开启权限 self?.alertSetAuthority(type: .photos) result(false) } return } // 提示用户开启权限 alertSetAuthority(type: .photos) result(false) } // MARK:- 获取相机权限 func getCameraAuthority(result: @escaping resultBlock) { let status = AVCaptureDevice.authorizationStatus(for: .video) if status == .authorized { result(true) return } if status == .notDetermined { AVCaptureDevice.requestAccess(for: .video) { [weak self] granted in result(granted) if !granted { // 提示用户开启权限 self?.alertSetAuthority(type: .camera) } } return } // 提示用户开启权限 alertSetAuthority(type: .camera) result(false) } } // MARK:- 定位权限协议 extension SystemAuthorityFireBoltt: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { switch status { case .restricted, .denied: // 提示用户开启权限 resultAAA!(false) break case .authorizedAlways, .authorizedWhenInUse: resultAAA!(true) break case .notDetermined: break default: break } } } // MARK:- 提示处理 extension SystemAuthorityFireBoltt { fileprivate func alertSetAuthority(type: AuthorityType) { var title: String = "" switch type { case .location: break case .photos: title = MultiLanguageKey_FB.photoTipFB.localized case .camera: title = MultiLanguageKey_FB.cameraiTipFB.localized } if let vc = keyWindow.rootViewController { showAlert(vc, title, cancelText: MultiLanguageKey_FB.cancelFB.localized, confirmText: MultiLanguageKey_FB.openFB.localized) { (result) in if result { if let url = URL(string: UIApplication.openSettingsURLString) { if UIApplication.shared.canOpenURL(url) { UIApplication.shared.openURL(url) } } } } } } }