OGeek|极客世界-中国程序员成长平台

标题: ios - swift中相机和照片库的隐私问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:14
标题: ios - swift中相机和照片库的隐私问题

我的应用最初在各种设备上访问相机和照片库都没有问题。

现在我发现在某些设备上我无法访问相机或照片库,并且在我尝试访问相机和照片后,该应用根本没有出现在隐私设置中。

无论我做什么,我都无法让 IOS 识别我的应用。

如何访问相机和照片库,并让我的应用出现在隐私设置中? 代码:

@IBAction func openCameraButton(sender: AnyObject) {
      if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == true {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .Camera//UIImagePickerControllerSourceType.Camera;
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }else{
        noCamera()

    }
}
@IBAction func openPhotoLibraryButton(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) == true {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        imagePicker.allowsEditing = true
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }else{
        noAccess()
    }
}



Best Answer-推荐答案


我使用此代码检查可访问性并在需要时请求它:

import AVFoundation
import Photos

func getCameraAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
    let authorizationState = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
    switch authorizationState {
    case .NotDetermined:
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (didAllow) in
            completion(isAccesible: didAllow)
        })
    case .Restricted:
        completion(isAccesible: false)
    case .Denied:
        completion(isAccesible: true)
    case .Authorized:
        completion(isAccesible: true)
 }
}

func getPhotoRollAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
    PHPhotoLibrary.requestAuthorization { (status) in
        switch status {
        case .Authorized:
            completion(isAccesible: true)
        case .Restricted, .Denied , .NotDetermined:
            completion(isAccesible: false)
        }
    }
}

用法:

self.getCameraAccessibilityAndRequestIfNeeded { (isAccesible) in
            if isAccesible {
                // Access confirmed
            } else {
                // No access
            }
        }

        self.getPhotoRollAccessibilityAndRequestIfNeeded { (isAccesible) in
            if isAccesible {
                // Access confirm
            } else {
                // No access
            }
        }

关于ios - swift中相机和照片库的隐私问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38904468/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4