试着写下这句话:

if usergavepermissiontousercamera  
  opencamera
else 
  showmycustompermissionview

找不到执行此简单任务的当前方法

推荐答案

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

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
       }
   })
}

NOTE:

  1. 确保在构建阶段的链接二进制部分添加AVFoundation Framework
  2. 对于导入AVFoundation,您应该在您的班级上写import 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
      }
   })
}

Swift 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
        }
    })
}

Ios相关问答推荐

找不到com.google.vr:sdk-base:1.180.0

如何让AVAudio.installTap()每秒回调125次

SwiftUI中ForEach和@ State数组的可能并发问题

如何在SWIFT中处理不可发送的类型?

Xamarin Forms CustomPin iOS Render

在SwiftUI中插入绘制重复形状

在TVOS中访问SWIFT中的objc++函数时发送给类的无法识别的 Select 符

如何防止UITest套件与Fastlane一起执行?

为什么AVSpeechSynthesizer复制的信息比我的文本多?

flutter中实现自定义底部导航栏

当目标位于菜单标签内时,matchedGeometryEffect 的行为很奇怪

iOS设备网页自动化中,在向字段发送文本后未启用按钮

swiftUI中的剪辑形状无法使用旋转效果

如何 comments .plist 文件中的行?

界面生成器 - 无法从路径加载设计对象(空)

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

在 UITextView 中垂直居中文本

在 React Native 中保存敏感数据

在 iOS 上编写文件

我可以在 UIScrollView 中使用 UIRefreshControl 吗?