import UIKit
import AVKit
import Photos
struct PermissionsHelper {
static func cameraEnable() -> Bool {
func cameraResult() {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
if (authStatus == .authorized) {
saveCamera(value: "1")
} else if (authStatus == .denied) {
saveCamera(value: "0")
let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相机] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
} else if (authStatus == .restricted) {
saveCamera(value: "0")
let alertV = UIAlertView.init(title: "提示", message: "相机权限受限", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
} else if (authStatus == .notDetermined) {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
if statusFirst {
saveCamera(value: "1")
} else {
saveCamera(value: "0")
}
})
}
}
func saveCamera(value: String) {
UserDefaults.standard.setValue(value, forKey: "cameraEnablebs")
}
cameraResult()
let result = (UserDefaults.standard.value(forKey: "cameraEnablebs") as? String) == "1"
return result
}
static func audioEnable() -> Bool {
func audioResult() {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
if (authStatus == .authorized) {
saveAudio(value: "1")
} else if (authStatus == .denied) {
saveAudio(value: "0")
let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 麦克风] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
} else if (authStatus == .restricted) {
saveAudio(value: "0")
let alertV = UIAlertView.init(title: "提示", message: "相机权限受限", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
} else if (authStatus == .notDetermined) {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
if statusFirst {
saveAudio(value: "1")
} else {
saveAudio(value: "0")
}
})
}
}
func saveAudio(value: String) {
UserDefaults.standard.setValue(value, forKey: "audioEnablebs")
}
audioResult()
let result = (UserDefaults.standard.value(forKey: "audioEnablebs") as? String) == "1"
return result
}
static func photoEnable() -> Bool {
func photoResult() {
let status = PHPhotoLibrary.authorizationStatus()
if (status == .authorized) {
savePhoto(value: "1")
} else if (status == .restricted || status == .denied) {
let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相册] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
savePhoto(value: "0")
} else if (status == .notDetermined) {
PHPhotoLibrary.requestAuthorization({ (firstStatus) in
let isTrue = (firstStatus == .authorized)
if isTrue {
savePhoto(value: "1")
} else {
savePhoto(value: "0")
}
})
}
}
func savePhoto(value: String) {
UserDefaults.standard.setValue(value, forKey: "photoEnablebs")
}
photoResult()
let result = (UserDefaults.standard.object(forKey: "photoEnablebs") as? String) == "1"
return result
}
}
|