2016-08-11 92 views
2

我的应用程序最初在各种设备上访问相机和照片库时都没有问题。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() 
    } 
} 
+0

用相关代码更新您的问题。在尝试访问摄像头和照片库后,您对应用的“设置”页面有什么看法? – rmaddy

+0

我使用以下代码: @IBAction FUNC openCameraButton(发件人:AnyObject){ \t \t如果UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)==真{ \t \t \t让imagePicker =的UIImagePickerController() \t \t \t imagePicker .delegate = self \t \t \t imagePicker.sourceType = .Camera // UIImagePickerControllerSourceType.Camera; \t \t \t imagePicker.allowsEditing =假 \t \t \t self.presentViewController(imagePicker,动画:真,完成:无) \t \t}否则{ \t \t \t noCamera() \t \t \t \t } \t \t } 对于相机和相似的照片 – Jeremy

回答

0

我使用此代码为通俗易懂的检查并在必要时要求其:

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 
      } 
     } 
+0

谢谢 我知道必须导入AVFounda但我不知道如何调用函数,即每种情况下的参数是什么 – Jeremy

+0

@Jeremy我编辑了我的答案以显示如何使用。而论点是“完成处理程序”。您可以将其用于网络请求等异步任务,或者在此示例中请求可访问性。你可以在这里阅读:https://thatthinginswift.com/completion-handlers/ – MCMatan

0

最终的答案很简单:

我在做imagePicker =按钮操作中的UIImagePickerController()将它移动到类名称下方,并且摄像头权限返回。

0

我现在已经在所有IOS设备上测试了我的代码 - 在实际设备上iPhone 4s和iPod 5触摸,其余模拟器 - 继续不允许访问照片库的唯一设备,也不显示该应用程序隐私是iPhone 6的iPad Air 2和视网膜 - 所以它看起来也许也适用于显示器。

有没有其他人有这个问题?