1憎兽、相冊權(quán)限校驗(yàn)冷离,代碼如下:
//相冊
func checkPhotoLibraryPermission() -> Bool{
if #available(iOS 9.0, *){ // iOS 9 及其以上系統(tǒng)運(yùn)行
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .notDetermined: //用戶尚未做出選擇
return false
case .authorized: //已授權(quán)
return true
case .denied: //用戶拒絕
return false
case .restricted: //家長控制
return false
}
}else {
let authorizationStatus = ALAssetsLibrary.authorizationStatus()
switch authorizationStatus {
case .notDetermined: //用戶尚未做出選擇
return false
case .authorized: //已授權(quán)
return true
case .denied: //用戶拒絕
return false
case .restricted: //家長控制
return false
}
}
}
2、相機(jī)權(quán)限校驗(yàn)唇兑,代碼如下:
//相機(jī)
func checkCameraPermission() -> Bool{
let mediaType = AVMediaTypeVideo
let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: mediaType)
switch authorizationStatus {
case .notDetermined: //用戶尚未做出選擇
return false
case .authorized: //已授權(quán)
return true
case .denied: //用戶拒絕
return false
case .restricted: //家長控制
return false
}
}
3酒朵、麥克風(fēng)權(quán)限校驗(yàn),代碼如下:
//麥克風(fēng)
func checkMicroPermission() -> Bool{
let mediaType = AVMediaTypeAudio
let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: mediaType)
switch authorizationStatus {
case .notDetermined: //用戶尚未做出選擇
return false
case .authorized: //已授權(quán)
return true
case .denied: //用戶拒絕
return false
case .restricted: //家長控制
return false
}
}
4扎附、定位權(quán)限校驗(yàn)蔫耽,代碼如下:
//定位
func checkLocationPermission() -> Bool{
let status = CLLocationManager.authorizationStatus()
switch status {
case .notDetermined: //用戶尚未做出選擇
return false
case .restricted: //未被授權(quán)
return false
case .denied: //用戶拒絕
return false
case .authorizedWhenInUse: //使用期間定位
return true
case .authorizedAlways: //一直定位
return true
}
}
為什么switch最后沒有default呢?因?yàn)槲野袮VAuthorizationStatus所有狀態(tài)都列出來了
public enum AVAuthorizationStatus : Int {
case notDetermined
case restricted
case denied
case authorized
}