一尘不染

如何检查用户是否授予使用相机的权限?

swift

尝试写这个:

if usergavepermissiontousercamera  
  opencamera
else 
  showmycustompermissionview

找不到执行此简单任务的当前方法。
注意:即使需要其他方法,iOS7也应该可以使用


阅读 266

收藏
2020-07-07

共1个答案

一尘不染

您可以使用以下代码执行相同的操作:

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
    // Already Authorized
} else {
    AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
       if granted == true {
           // User granted
       } else {
           // User rejected
       }
   })
}

注意:

  1. 确保AVFoundation在构建阶段的“链接二进制”部分中添加框架
  2. 您应该import AVFoundation在课堂上写一些用于导入的内容AVFoundation

SWIFT 3

if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) ==  AVAuthorizationStatus.authorized {
   // Already Authorized
} else {
   AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
      if granted == true {
         // User granted
      } else {
         // User Rejected
      }
   })
}

斯威夫特4

if AVCaptureDevice.authorizationStatus(for: .video) ==  .authorized {
    //already authorized
} else {
    AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
        if granted {
            //access allowed
        } else {
            //access denied
        }
    })
}
2020-07-07