Presenting camera permission dialog in iOS 8

后端 未结 9 1581
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 17:45

When my app tries to access the camera for the first time on iOS 8, the user is presented with a camera permission dialog, much like the microphone one for microphone access

9条回答
  •  臣服心动
    2020-11-29 18:23

    For Swift 3, you can add this on your viewWillAppear method of your first view controller:

    First import the AVFoundation framework

    import AVFoundation
    

    Then:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
    
        switch authorizationStatus {
        case .notDetermined:
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
                if granted {
                    print("access granted")
                }
                else {
                    print("access denied")
                }
            }
        case .authorized:
            print("Access authorized")
        case .denied, .restricted:
            print("restricted")
    
        }
    }
    

    Don't forget to add Privacy - Camera Usage Description key on your Info.plist

提交回复
热议问题