title: Export file from iOS app in swift
body: <p>I'm fairly new to Swift and iOS development in general. My app has a model that can easily be expressed as comma separated values (csv), so naturally I want the user to be able to export the data as an csv file and opening that file in another application. Since I didn't find any examples in Swift, I gave it a try on my own:</p>
  
  <pre><code>func ExportToCSV(delegate: UIDocumentInteractionControllerDelegate){
      let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.csv")
      let url: NSURL! = NSURL(fileURLWithPath: fileName)
  
      var data  = "Date,Time,Count\n2014-11-21,14.00,42"
  
      data.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
      if url != nil {
          let docController = UIDocumentInteractionController(URL: url)
          docController.UTI = "public.comma-separated-values-text"
          docController.delegate = delegate
          docController.presentPreviewAnimated(true)
      }
  }
  </code></pre>
  
  <p>(the delegate parameter is the view that calls the function, as in <code>MyClass.ExportToCSV(self)</code>)</p>
  
  <p>This works, mostly, and I see the following views:</p>
  
  <p><img src="https://i.stack.imgur.com/B8lBe.png" width="300">
  <img src="https://i.stack.imgur.com/9jq5a.png" width="300"></p>
  
  <p>However, in the Simulator I get the following warning:</p>
  
  <pre><code>Unbalanced calls to begin/end appearance transitions for &lt;QLRemotePreviewContentController: 0x7fcd720da800&gt;.
  </code></pre>
  
  <p>as well as </p>
  
  <pre><code>Unknown activity items supplied: ("&lt;QLPrintPageRenderer: 0x7fcd73861ee0&gt;","&lt;UIPrintInfo: 0x7fcd714b9030&gt;")
  </code></pre>
  
  <p>when I click the <em>action</em> button, then after a while</p>
  
  <pre><code>Communications error: &lt;OS_xpc_error: &lt;error: 0x10e032b10&gt; { 
      count = 1, contents = "XPCErrorDescription" 
      =&gt; &lt;string: 0x10e032f18&gt; { length = 22, contents = "Connection interrupted" }
  }&gt;
  </code></pre>
  
  <p>and when I click <em>Mail</em> there is a crash with the following error:</p>
  
  <pre><code>viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain 
      Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" 
      UserInfo=0x7fcd71631460 {Message=Service Connection Interrupted}
  &lt;MFMailComposeRemoteViewController: 0x7fcd73864aa0&gt; timed out waiting for fence 
      barrier from com.apple.MailCompositionService
  </code></pre>
  
  <p>Although on the actual device everything works as planned, so many errors throw me off a bit. Is there a better solution?</p>