判断系统权限是否开启
1.定位权限
//引入头文件
import CoreLocation
//定位权限判断
switch CLLocationManager.authorizationStatus() {
case .denied :
//未授权
case .notDetermined :
//不确定
case .authorizedAlways :
//一直允许
case .authorizedWhenInUse :
//使用期间允许
case .restricted :
//受限的
}
2.相机权限
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .denied :
//未授权
case .notDetermined :
//不确定
case .authorizedAlways :
//一直允许
case .authorizedWhenInUse :
//使用期间允许
case .restricted :
//受限的
}
3.相册权限
import Photos
switch PHPhotoLibrary.authorizationStatus() {
case .denied :
//未授权
case .notDetermined :
//不确定
case .authorizedAlways :
//一直允许
case .authorizedWhenInUse :
//使用期间允许
case .restricted :
//受限的
4.通知权限
import UserNotifications
UNUserNotificationCenter.current().getNotificationSettings { setting in
switch setting.authorizationStatus {
case .denied :
//未授权
case .notDetermined :
//不确定
case .authorizedAlways :
//一直允许
case .authorizedWhenInUse :
//使用期间允许
case .restricted :
//受限的
}
5.蓝牙权限
import CoreBluetooth
switch CBPeripheralManager.authorization {
case .denied :
//未授权
case .notDetermined :
//不确定
case .allowedAlways :
//一直允许
case .restricted :
//受限的
}
6.网络权限
switch CTCellularData().cellularDataRestrictionDidUpdateNotifier {
case .notRestricted:
//无限制(允许无线局域网与蜂窝数据时)
case .restricted:
//受限的(仅允许无线局域网时、权限关闭时)
case .restrictedStateUnknown:
//受限状态未知的
}
ps:网络权限获取会较慢,需进行信号量进行异步操作获取,可使用下方方法进行获取当前网络权限
///判断网络是否有权限
private func isNetworkPermissions() -> Bool {
var isNetworkPermissions:Bool = false
let cellularData = CTCellularData()
///线程信号量
let semaphore = DispatchSemaphore(value: 0)
cellularData.cellularDataRestrictionDidUpdateNotifier = { state in
if state == .notRestricted {
isNetworkPermissions = true
} else {
isNetworkPermissions = false
}
semaphore.signal()
}
semaphore.wait()
return isNetworkPermissions
}
后续如果有其他权限判断,再进行添加