2

My app initially had no problems accessing both camera and photo library on various devices.

Now I find that on some devices I can't get access to the camera or photo library, and the app does not appear at all in the privacy settings after I have tried to access camera and photos.

No matter what I do I can't get IOS to recognize my app.

What can I do to to access the camera and photo library, and have my app appear in the privacy settings? code:

@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()
    }
}
Jeremy
  • 145
  • 1
  • 2
  • 13
  • Update your question with relevant code. And what do you see for your app's Settings page after trying to access the camera and photo library? – rmaddy Aug 11 '16 at 20:37
  • I am using the following code: @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() } } For camera and similar for photos – Jeremy Aug 12 '16 at 06:01

3 Answers3

0

I use this code for checking accessibly and requesting it if needed:

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

Usage:

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

        self.getPhotoRollAccessibilityAndRequestIfNeeded { (isAccesible) in
            if isAccesible {
                // Access confirm
            } else {
                // No access
            }
        }
MCMatan
  • 8,623
  • 6
  • 46
  • 85
  • Thank you I know one must import AVFoundation but I am not sure how to call the functions i.e. what is the argument in each case – Jeremy Aug 12 '16 at 06:17
  • @Jeremy I've edited my answer to show how the usage. And the argument is a "Completion Handlers". You use this for async tasks like network requests or in this example asking for accessibility. You can read about it here: https://thatthinginswift.com/completion-handlers/ – MCMatan Aug 12 '16 at 12:48
0

In the end the answer was very simple:

I was doing let imagePicker = UIImagePickerController() within the button actions moved it to below class name and camera permissions were back.

Jeremy
  • 145
  • 1
  • 2
  • 13
0

I have now tested my code on all IOS devices - on actual devices iPhone 4s and iPod 5 touch and the rest on simulators - the only devices that continue to not allow access to Photo Library and also do not show the app in Privacy are the iPhone 6's iPad Air 2 and retina - so it seems maybe its too do with the display.

Has anyone else had this problem?

Jeremy
  • 145
  • 1
  • 2
  • 13