title: Using UIDocumentPickerViewController to import text in Swift
body: <p>I'm currently taking an iOS development course and as part of my project, I'm tasked with using <code>UIDocumentPickerViewController</code> to import text. Every example I've found is either a) written in Objective-C or b) is for importing <code>UIImage</code> files.</p>
  
  <p><strong>How do I set the delegate method for a text file?</strong></p>
  
  <p><em>Here's what I've got so far:</em></p>
  
  <p>I have the iCloud capability set up. <strong>Works</strong></p>
  
  <p>The delegate is specified, as follows:</p>
  
  <pre><code>class MyViewController: UIViewController, UITextViewDelegate, UIDocumentPickerDelegate
  </code></pre>
  
  <p>I have the property set up for the type of text I'm trying to import:</p>
  
  <pre><code>@IBOutlet weak var newNoteBody: UITextView!
  </code></pre>
  
  <p>I have an <code>IBAction</code> setup as follows:</p>
  
  <pre><code>@IBAction func importItem(sender: UIBarButtonItem) {
  
      var documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: [kUTTypeText as NSString], inMode: UIDocumentPickerMode.Import)
      documentPicker.delegate = self
      documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
      self.presentViewController(documentPicker, animated: true, completion: nil)
  
  }
  </code></pre>
  
  <p>I can't figure out what the line should be below. The documentation on Apple's website isn't clear and every example I've found is in Objective-C or is for images.</p>
  
  <pre><code>// MARK: - UIDocumentPickerDelegate Methods
  
  func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
      if controller.documentPickerMode == UIDocumentPickerMode.Import {
          // What should be the line below?
          self.newNoteBody.text = UITextView(contentsOfFile: url.path!)
      }
  }
  </code></pre>