AppDelegate中的方法
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//MARK: APP启动时候,判断用户是否授权使用相机
if (AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == .notDetermined) {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
if statusFirst {
//用户首次允许
print("允许APP访问相机")
} else {
//用户首次拒接
print("拒绝APP访问相机")
}
})
}
//MARK: APP启动时候,判断用户是否授权使用相册
if (PHPhotoLibrary.authorizationStatus() == .notDetermined) {
PHPhotoLibrary.requestAuthorization({ (firstStatus) in
let result = (firstStatus == .authorized)
if result {
print("允许APP访问相册")
} else {
print("拒绝APP访问相册")
}
})
}
return true
}
相机、相册权限判断
/****************************APP权限**************************************/
/* .restricted ---> 受限制,系统原因,无法访问
* .notDetermined ---> 系统还未知是否访问,第一次开启时
* .authorized ---> 允许、已授权
* .denied ---> 受限制,系统原因,无法访问
*/
//MARK: 判断是否可访问相册
//UIDevice.current.systemVersion >= 8.0
///相册权限---> 直接使用 <Photo方法 @available(iOS 8.0, *)>
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 {
print("首次允许")
savePhoto(value: "1")
} else {
print("首次不允许")
savePhoto(value: "0")
}
})
}
}
func savePhoto(value: String) {
UserDefaults.standard.setValue(value, forKey: "photoEnablebs")
}
let result = (UserDefaults.standard.object(forKey: "photoEnablebs") as! String) == "1"
return result
}
///相册权限 --> 需要AppleDelegate中让用户先选择授权
func photoEnableDelegate() -> Bool {
let status = PHPhotoLibrary.authorizationStatus()
if status == .authorized {
return true
}
return false
}
//UIDevice.current.systemVersion < 8.0
//depressed
///相册权限---> 需要AppleDelegate中让用户先选择授权, 此方法将被废弃 <AssetsLibrary方法 @available(iOS 7.0, *)>
func photoEnableDelegate2() -> Bool {
let author = ALAssetsLibrary.authorizationStatus()
print("--\(author.rawValue)--")
if (author == .restricted || author == .denied) {
let alertV = UIAlertView.init(title: "提示", message: "请去-> [设置 - 隐私 - 相册] 打开访问开关", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定")
alertV.show()
return false
}
return true
}
//MARK: 判断是否可访问相机
///相机权限 ---> 直接调用
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
}
///相机权限2 --> 需要AppleDelegate中让用户先选择授权
func cameraEnableDelegate() -> Bool {
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
if status == .authorized {
return true
}
return false
}
/******************************************************************/