Tag Archives: File management

How to export a file to iPhone documents folder programmatically

Here I give an example of how to export a file to iPhone documents folder programmatically. I spent a lot of time on managing of how to do it. My main problem was that I couldn’t find documents folder of iPhone simulator. I couldn’t write anything to file and I used a wrong way with NSData class. Finally, I found a recipe in Internet and modified it a little bit.

So look at the example:

This is just a method. Firstly, we discover a documents Directory of a device. Then we NSLog it in order to find that directory on Mac, if we use iPhone/iPad simulator. Then we make a file name to write the data to using the documents directory. Then create content – four lines of text, including even some non ASCII text(that’ the modification). Then we just save content to the documents directory. Now that file is available through iTunes in programs tab, in the bottom of it for the given Applicaton. Note that you should add “Application supports iTunes file sharing” line and check it in your Info.plist file for your application in order to be able to use iTunes/iPhone/Programs/Your app to share files. Also note that encoding:NSUTF8StringEncoding is needed if you want to use non ASCII letters.