iOS相册、相机权限判断 (Swift 4)

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
}

/******************************************************************/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值