3

enter image description here

I tried setting CFBundleDisplayName and Privacy - Photo Library Usage Description but still not working

But in the settings it has permission on/off

I tried uninstalling the app and installing again but it never asks permission if it is off it shows the above screenshot...user has to go to settings and turn it on.

so how to make it ask for permission on first use.

Here is the code that I am using

let imgPicker:UIImagePickerController=UIImagePickerController()
imgPicker.delegate=self
imgPicker.sourceType=UIImagePickerControllerSourceType.PhotoLibrary
imgPicker.mediaTypes=[kUTTypeImage as String,kUTTypeMovie as String]
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(imgPicker, animated: true, completion: nil)
Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241

1 Answers1

0

I just created an extension to do this you should be able to just add it to your code and hook up the outlet to what ever triggers the image picker in your app.

   // MARK: - UIImagePickerControllerDelegate Methods
extension UIViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate{

@IBAction func newImgTapped(sender: UIButton) {
    let imgPicker = UIImagePickerController()
    imgPicker.delegate = self
    let status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
    if status == AVAuthorizationStatus.Denied{

        let changeYourSettingsAlert = UIAlertController(title: "You do not have permissions enabled for this.", message: "Would you like to change them in settings?", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: .Default, handler: { (UIAlertAction) -> Void in
            guard let url = NSURL(string: UIApplicationOpenSettingsURLString) else {return}
            UIApplication.sharedApplication().openURL(url)
            print("opensettings")

        })
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

        changeYourSettingsAlert.addAction(okAction)
        changeYourSettingsAlert.addAction(cancelAction)
        presentAlert(changeYourSettingsAlert)


    } else {
        let Alert = UIAlertController(title: "Where would you like to get photos from?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        Alert.popoverPresentationController?.sourceRect = sender.bounds
        Alert.popoverPresentationController?.sourceView = sender
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

        imgPicker.allowsEditing = true
        imgPicker.modalPresentationStyle = UIModalPresentationStyle.Popover
        imgPicker.popoverPresentationController?.sourceView = sender
        imgPicker.popoverPresentationController?.sourceRect = sender.bounds

        presentAlert(Alert)

        let camera = UIAlertAction(title: "Take a Photo", style: .Default) { (camera) -> Void in
            imgPicker.sourceType = .Camera
            self.presentViewController(imgPicker, animated: true, completion: nil)
        }

        let photoLibrary = UIAlertAction(title: "Choose from Library", style: .Default) { (Photolibrary) -> Void in

            imgPicker.sourceType = .PhotoLibrary
            self.presentViewController(imgPicker, animated: true, completion: nil)

        }

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            Alert.addAction(camera)
        }
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
            Alert.addAction(photoLibrary)
        }
        Alert.addAction(cancelAction)
    }
}

public func presentAlert(sender:UIAlertController){
    presentViewController(sender, animated: false, completion: nil)
}

public func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}
}

hope that helps.

KVISH
  • 12,923
  • 17
  • 86
  • 162
Dan Leonard
  • 3,325
  • 1
  • 20
  • 32