摄像机权限判断问题包括第一次允许判断

本文介绍了如何在iOS应用中进行摄像机和麦克风权限的判断与请求。通过使用AVCaptureDevice的authorizationStatusForMediaType方法检查权限状态,并使用requestAccessForMediaType来请求用户授权。当用户首次使用或之后改变权限时,根据权限状态显示相应提示或执行相应操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做了一个视频功能涉及到摄像机和麦克风的权限问题所以写一个权限判断

下面贴代码 以下是摄像机 和  麦克风的权限 设置 分别为 第一次使用的情况   和 以后每次使用时候 设备的检测情况 

 func authStatus()

    {

       

        let videoType = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)

        let audioType = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)


        switch videoType {

        case .NotDetermined:

            AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (isCanUse) in

                if isCanUse == true

                {

                    //用户同意则

                    dispatch_async(dispatch_get_main_queue(), { 

                       

                    })

                }

                else

                {

                    //用户拒绝回调不在主线程  要加入主线程

                    dispatch_async(dispatch_get_main_queue(), { 

//这边做了一个返回的操作  也可以留在当前页面做其他操作

                        if self.navigationController != nil

                        {

                            self.navigationController?.popViewControllerAnimated(true)

                        }

                    })

                }

                

                

            })

        case .Denied: //用户拒绝

            dispatch_async(dispatch_get_main_queue(), { 

                 self.tips("未获得授权使用摄像头", message: "请在iOS'设置'-'隐私'-'相机'中打开")

            })

           

         

        case .Restricted: //无法访问

            dispatch_async(dispatch_get_main_queue(), { 

                 self.function.showReadTip("无法访问摄像机", isCount: false)

            })

           

        case .Authorized:

            print("启动成功")

            

        default: break

            

        }

        

        switch audioType {

        case .NotDetermined:

            AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (isCanUse) in

                if isCanUse == true

                {

                    //用户同意则

                    dispatch_async(dispatch_get_main_queue(), {

                       

                    })

                }

                else

                {

                    //用户拒绝回调不在主线程  要加入主线程

                    dispatch_async(dispatch_get_main_queue(), {

                        if self.navigationController != nil

                        {

                            self.navigationController?.popViewControllerAnimated(true)

                        }

                    })

                }

                

            })

        case .Denied: //用户拒绝

            dispatch_async(dispatch_get_main_queue(), {

                self.tips("未获得授权使用麦克风", message: "请在iOS'设置'-'隐私'-'麦克风'中打开")

            })

            

            

        case .Restricted: //无法访问

            dispatch_async(dispatch_get_main_queue(), {

                self.function.showReadTip("无法访问摄像机", isCount: false)

            })

            

        case .Authorized:

            print("启动成功")

            

        default: break

            

        }


        }

        



 //提示框

    func tips(title:String,message:String)

    {

        //title: "未获得授权使用摄像头", message: "请在iOS'设置'-'隐私'-'相机'中打开"

        //提示相机不可用

        if #available(iOS 8.0, *) {

            let alert = UIAlertController.init(title: title, message: message, preferredStyle: .Alert)

            let confirm = UIAlertAction.init(title: "知道了", style: .Cancel, handler: { (cofirm) in

                if self.navigationController != nil

                {

                    self.navigationController?.popViewControllerAnimated(true)

                }

            })

            let nowGo = UIAlertAction.init(title: "现在就去", style: .Default, handler: { (nowGo) in

//                let url = NSURL.init(string: "prefs:root=privacy")

                let url = NSURL.init(string: UIApplicationOpenSettingsURLString)

                if UIApplication.sharedApplication().canOpenURL(url!)

                {

                    UIApplication.sharedApplication().openURL(url!)

                    if self.navigationController != nil

                    {

                        self.navigationController?.popViewControllerAnimated(false)

                    }

                }

                else

                {

                    if self.navigationController != nil

                    {

                        self.navigationController?.popViewControllerAnimated(true)

                    }

                }

            })

            alert.addAction(confirm)

            alert.addAction(nowGo)

            self.presentViewController(alert, animated: true, completion: nil)

            

        } else {

           

        }

        

    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值