-1

I've tried to put a documentPicker to get a file from iCloud but Actually my app opens the documentMenu to import a file (iCloud, Dropbox) and when I choose iCloud, I present the document picker with my files. When I've to delegate the file to documentPicker(_ ...) [documentPicker.delegate=self.delegate] function is never called because my class does not conform to protocol

import UIKit

class ImportKeyViewController: UIViewController{
    @IBOutlet weak var openWithLabel: UILabel!
    @IBOutlet weak var transparentBackgroundView: UIView!
    @IBOutlet weak var iCloudButton: UIButton!
    @IBOutlet weak var iTunesButton: UIButton!

    weak var delegate : UIDocumentPickerDelegate?

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){
        print("Entre a documentPicker")
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
       print("Sali a documentPicker")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("llegue")
        setUpUI()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIView.animate(withDuration: 0.1, animations: {
            self.transparentBackgroundView.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 0.7)
        })
    }

    override func viewWillDisappear(_ animated: Bool) {
        UIView.animate(withDuration: 0.1, animations: {
            self.transparentBackgroundView.backgroundColor = UIColor.clear
        })
    }

    @IBAction func closeButtonAction(_ sender: UIButton) {

        let documentPicker = UIDocumentPickerViewController (documentTypes: ["public.text","public.content"], in: .import)
        documentPicker.delegate? = self.delegate!
        self.present(documentPicker, animated:true, completion: nil)

    }
}

I've the obligatory and optional methods for UIDocumentPickerDelegate but it's not working as you can see in image below

This is my first iOS App, hope you can help me.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Elizabeth Hernandez
  • 149
  • 1
  • 1
  • 12

1 Answers1

0

You should add the new syntax of the didPickDocumentAt: delegate method, looks like it's changed a bit.

-- edit

Your ImportKeyViewController should inherit the UIDocumentPickerDelegate. A nice way to do this is by creating an extension, that way it's easily noticeable what methods are delegate methods, and what are your methods.

Like so:

extension ImportKeyViewController: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){
        print("Entre a documentPicker")
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
       print("Sali a documentPicker")
    }

}
Erik Terwan
  • 2,710
  • 19
  • 28
  • This one? func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){...} – Elizabeth Hernandez Feb 23 '17 at 22:33
  • In the image you posted, but removed now, you could notice the compiler errors. About your viewcontroller not confirming to the delegate. You can check out the delegate methods by hitting command and clicking on the UIDocumentPickerDelegate. – Erik Terwan Feb 23 '17 at 22:51
  • Ah now i see what you're doing wrong, let me edit my answer. – Erik Terwan Feb 23 '17 at 22:51