UIImagePickerController也没有在第一次使用时请求权限

时间:2016-03-09 05:02:48

标签: ios swift permissions uiimagepickercontroller photolibrary

enter image description here

我尝试设置CFBundleDisplayName隐私 - 照片库使用说明但仍无法正常工作

但是在设置中它具有开/关权限

我尝试卸载该应用并再次安装,但它永远不会请求权限,如果它关闭它会显示上面的屏幕截图...用户必须转到设置并打开它。

所以如何让它在首次使用时获得许可。

以下是我正在使用的代码

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)

1 个答案:

答案 0 :(得分:0)

我刚刚创建了一个扩展程序来执行此操作,您应该只需将其添加到代码中,然后将插座连接到应用程序中触发图像选择器的位置。

   // 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)
}
}
希望有所帮助。