Export file from iOS app to swift

I am new to Swift development and iOS in general. My application has a model that can be easily expressed as comma separated values ​​(csv), so naturally I want the user to be able to export the data as a csv file and open that file in another application. Since I didn't find any examples in Swift, I gave them a try on their own:

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)
    }
}

      

(the delegate parameter is the view that calls the function as in MyClass.ExportToCSV(self)

)

It basically works and I see the following views:

B8lBe.png9jq5a.png

However, in the Simulator I get the following warning:

Unbalanced calls to begin/end appearance transitions for <QLRemotePreviewContentController: 0x7fcd720da800>.

      

and

Unknown activity items supplied: ("<QLPrintPageRenderer: 0x7fcd73861ee0>","<UIPrintInfo: 0x7fcd714b9030>")

      

when i press the action button and after a while

Communications error: <OS_xpc_error: <error: 0x10e032b10> { 
    count = 1, contents = "XPCErrorDescription" 
    => <string: 0x10e032f18> { length = 22, contents = "Connection interrupted" }
}>

      

and when I click "Mail" it crashes with the following error:

viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain 
    Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" 
    UserInfo=0x7fcd71631460 {Message=Service Connection Interrupted}
<MFMailComposeRemoteViewController: 0x7fcd73864aa0> timed out waiting for fence 
    barrier from com.apple.MailCompositionService

      

Although on the device itself, everything works as planned, so many of the errors threw me a little back. Is there a better solution?

+3


source to share


1 answer


This is likely because the objects have already been freed when accessing the mail or print application. Try to declare variables docController

and url

as properties of the class so that they persist as long as the view controller exists.



+1


source







All Articles