<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iOS Developer Tips Blog &#187; Data / File Management</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/data-file-management/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Delete All Files in Documents Directory</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html#comments</comments>
		<pubDate>Tue, 17 Jan 2012 02:09:15 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10941</guid>
		<description><![CDATA[In a recent project, I was using iTunes File Sharing to store various log files and test data. Although you can quickly delete all the files in the Documents directory using iTunes, I also was looking for a way to clean up the same directory when my device was not connected. The first approach I [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project, I was using <a href="http://iphonedevelopertips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html" target="_blank">iTunes File Sharing</a> to store various log files and test data. Although you can quickly delete all the files in the Documents directory using iTunes, I also was looking for a way to clean up the same directory when my device was not connected.</p>
<p>The first approach I took was to loop through all the files, building a path to each, one by one:<br />
<span id="more-10941"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Path to the Documents directory</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Print out the path to verify we are in the right place</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>directory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Directory: %@&quot;</span>, directory<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For each file in the directory, create full path and delete the file</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>fileManager contentsOfDirectoryAtPath<span style="color: #002200;">:</span>directory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>    
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>directory stringByAppendingPathComponent<span style="color: #002200;">:</span>file<span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File : %@&quot;</span>, filePath<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">BOOL</span> fileDeleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>filePath error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>fileDeleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code above works fine, however, I was interested in something that didn&#8217;t build each path separately &#8211; the code below deletes all the files in one fell swoop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Path: %@&quot;</span>, <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove Documents directory and all the files</span>
  <span style="color: #a61390;">BOOL</span> deleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>deleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Given the requested item to remove was a directory, as you&#8217;d expect, not only were the files deleted, so was the Documents directory. Problem is, I ran into errors when I attempted to drag/drop new files into the file sharing areas iTunes.</p>
<p>To get things working again, I changed up the code above to create the Documents directory:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Path: %@&quot;</span>, <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove all files in the documents directory</span>
  <span style="color: #a61390;">BOOL</span> deleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>deleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
    <span style="color: #11740a; font-style: italic;">// Recreate the Documents directory</span>
    <span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> withIntermediateDirectories<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Although there is less code to delete the Documents directory, something tells me it may simply be a better (and more robust) solution to loop and delete each file in sequence.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preview Documents Tutorial with QLPreviewController</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html#comments</comments>
		<pubDate>Mon, 09 May 2011 11:04:05 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8303</guid>
		<description><![CDATA[For the past few months I&#8217;ve been meaning to spend some time to check out the quick look document previewer on iOS &#8211; what follows is a short app that I wrote to get familiar with the QLPreviewController API. For those unfamiliar with the quick look previewer, it is a framework that provides quick previewing [...]]]></description>
			<content:encoded><![CDATA[<p>For the past few months I&#8217;ve been meaning to spend some time to check out the quick look document previewer on iOS &#8211; what follows is a short app that I wrote to get familiar with the <strong>QLPreviewController</strong> API.</p>
<p>For those unfamiliar with the quick look previewer, it is a framework that provides quick previewing of a range of document types &#8211; supported documents include iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.<br />
<span id="more-8303"></span></p>
<h5>Document Previewer Demo App</h5>
<p>What follows are a few screenshots of the previewer application covered in this post. I&#8217;ve added three different file types in the application, an image, a pdf and an MS Office document. The screenshot in the upper left is the list of available files to preview. In the lower left is Remodel.xls, an Excel document. The upper right screenshot is the iOSDevTips.png image. Notice in this same image in the upper right corner is a directional arrow (outlined with the red box), this leads you to one nice addition you get for free with preview, support for printing (assuming you have a wireless printer in the vicinity).</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/preview.jpg" alt="" width="590" height="500" /></p>
<h5>Document Previewer Interface</h5>
<p>The interface file for the application is shown below, notice the reference to the QL Data Source, when using the <strong>QLPreviewController</strong> you must adopt this protocol. The only instance variable is an array that will hold strings value representing the title of each document available for previewing. The <strong>UITableViewController</strong> class will be used to display the list of documents to preview.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;QuickLook/QuickLook.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TestViewController <span style="color: #002200;">:</span> UITableViewController &lt;QLPreviewControllerDataSource&gt;
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayOfDocuments;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Document Previewer Implementation</h5>
<p>In this section I&#8217;ll show a selection of code applicable to setting up the previewer. The code for creating the table view and populating the same can be viewed in the Xcode project which you can download from the link below.</p>
<p>The initialization code populates the array of document names:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    arrayOfDocuments <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span> 
        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iOSDevTips.png&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Remodel.xls&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Core J2ME Technology.pdf&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The method below is one of the two required when adopting the <strong>QLPreviewControllerDataSource</strong> protocol, this method informs the preview controller how many items are in the preview navigation list:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> numberOfPreviewItemsInPreviewController<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>QLPreviewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> controller 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>arrayOfDocuments count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The method that does all the work (other than the previewer itself of course) is shown below &#8211; this method returns a NSURL object of the item to display. Given the objects to display are packaged within the application, I create a path to the relevant file in the application bundle and return a URL using that same path.:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> &lt;QLPreviewItem&gt;<span style="color: #002200;">&#41;</span>previewController<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>QLPreviewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controller previewItemAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>index 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Break the path into its components (filename and extension)</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>fileComponents <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>arrayOfDocuments objectAtIndex<span style="color: #002200;">:</span> index<span style="color: #002200;">&#93;</span> componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;.&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Use the filename (index 0) and the extension (index 1) to get path</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>fileComponents objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> ofType<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>fileComponents objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The remaining code in the project is typical iPhone/iOS stuff, creating the app delegate, adding a subview (a navigation controller)  to the delegate&#8217;s <strong>UIWindow</strong> and making the window visible. I&#8217;ve included code from the delegate below, here you can get the bigger picture view of how I setup the view controller for this application.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>   
  <span style="color: #11740a; font-style: italic;">// Create and initialize the window</span>
  window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create test view controller</span>
  vc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create navigation controller </span>
  nav <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>vc<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>nav view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;  
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>One point worth mentioning is that you have two different options when working with a preview controller. First, you can push the object with the preview controller onto a view using a <strong>UINavigationController</strong> object, which you can see is what I&#8217;ve done here. The preview controller for my app lives in <strong>TestViewController</strong> and this object is set as the root view controller for a navigation controller.</p>
<p>The second approach to display the preview controller is modally, using the method <strong>presentModalViewController</strong>.</p>
<h5>Document Previewer Source Code</h5>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/Xcode-DocumentPreviewer.zip">Xcode project for iPhone Document Previewer</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Get Total and Free Space on the Mounted File System</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html#comments</comments>
		<pubDate>Mon, 14 Jun 2010 12:27:09 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6329</guid>
		<description><![CDATA[I recently was writing code to manage the download and storing of mp4 files to enable an application to play movies off-line. One consideration when dealing with files of significant size (50-100 megs) is disk space availability &#8211; this tip shows the method I called to get information about the mounted file system. Get Free [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was writing code to manage the download and storing of mp4 files to enable an application to play movies off-line. One consideration when dealing with files of significant size (50-100 megs) is disk space availability &#8211; this tip shows the method I called to get information about the mounted file system.</p>
<h5>Get Free Disk Space</h5>
<p>To get free space available, the class <strong>NSFileManager</strong> provides a method <strong>attributesOfFileSystemForPath:error:</strong> which will return a dictionary that includes five entries with various file system information.<br />
<span id="more-6329"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get access to a file manager as our means</span>
<span style="color: #11740a; font-style: italic;">// to perform file operations</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Using the application home directory, get dictionary of attributes</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>attributesDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager attributesOfFileSystemForPath<span style="color: #002200;">:</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Print total file system size and available space  </span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File system size: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File system free space: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Notice that the objects for the keys requested are <strong>NSNumbers</strong>. Calling the method <strong>longLongValue</strong> returns the value of the object as a long long type, which I use to print information to console.</p>
<p>For information on the additional attributes in the dictionary, check out the NSFileManager API documenation.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Read Files from the Application Bundle</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html#comments</comments>
		<pubDate>Wed, 31 Mar 2010 08:09:30 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6059</guid>
		<description><![CDATA[Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing. As an example of the later, often times I find it [...]]]></description>
			<content:encoded><![CDATA[<p>Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing.</p>
<p>As an example of the later, often times I find it helpful to put json (or xml) content into a file that I know is valid based on a remote server that I will eventually connect with. I add this file to the application bundle and read it into an internal data structure. I can then do various tests with known data, and once that is working, I can then proceed to the networking side of things to connect and retrieve live data.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Read in and store as string</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jsonTest&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;txt&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithContentsOfFile<span style="color: #002200;">:</span>file 
   encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
debug<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str: %@&quot;</span>, str<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Do something with the test data...</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Read in and store as raw data bytes</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;txt&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfFile<span style="color: #002200;">:</span>file2<span style="color: #002200;">&#93;</span>;</pre></div></div>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Save UIImage Object as a PNG or JPEG File</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html#comments</comments>
		<pubDate>Thu, 04 Mar 2010 13:38:18 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[jpg]]></category>
		<category><![CDATA[png]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[uiimage]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5640</guid>
		<description><![CDATA[In an earlier post on saving images from the camera to the Photo Album, a question was posed asking how to save the image to another directory other than the Photo Album. Let&#8217;s walk through one way to write a UIImage object, from the camera or otherwise, as a PNG or JPG file into the [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post on <a  href="http://iphonedevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html">saving images from the camera to the Photo Album</a>, a question was posed asking how to save the image to another directory other than the Photo Album. Let&#8217;s walk through one way to write a <strong>UIImage</strong> object, from the camera or otherwise, as a PNG or JPG file into the Documents directory.</p>
<h5>Get UIImage Data as PNG or JPEG</h5>
<p><strong>UIKit</strong> includes two C functions, <strong>UIImageJPEGRepresentation</strong> and <strong>UIImagePNGRepresentation</strong> which will return an <strong>NSData</strong> object that represents an image in JPEG or PNG format, respectively. With this information in hand, you can then use the <strong>writeToFile</strong> method of the <strong>NSData</strong> object to write the image data to a specified path.<br />
<span id="more-5640"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create paths to output images</span>
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>pngPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents/Test.png&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>jpgPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents/Test.jpg&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write a UIImage to JPEG with minimum compression (best quality)</span>
<span style="color: #11740a; font-style: italic;">// The value 'image' must be a UIImage object</span>
<span style="color: #11740a; font-style: italic;">// The value '1.0' represents image compression quality as value from 0.0 to 1.0</span>
<span style="color: #002200;">&#91;</span>UIImageJPEGRepresentation<span style="color: #002200;">&#40;</span>image, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span> writeToFile<span style="color: #002200;">:</span>jpgPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write image to PNG</span>
<span style="color: #002200;">&#91;</span>UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span> writeToFile<span style="color: #002200;">:</span>pngPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Let's check to see if files were successfully written...</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create file manager</span>
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Point to Document directory</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write out the contents of home directory to console</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>, <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Important note, on lines 6 and 9, the value <strong>image</strong> must be a <strong>UIImage</strong> object.</p>
<h5>Source Code</h5>
<p>I&#8217;ve changed the example <a  href="http://iphonedevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html">from the original post</a> that wrote a UIImage from the camera to the Photo Album, to write instead to the test files shown above. You can download the source code for the application here: <a href="http://iphonedevelopertips.com/wp-content/uploads/2010/03/camera2.zip">Write UIImage Object as a PNG or JPEG File</a>.</p>
<h5>Simulator Not Supported</h5>
<p>One last note, this application will only run on an actual device as the simulator does not have camera support.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Read and Write Array, Dictionary and Other Collections to Files</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html#comments</comments>
		<pubDate>Mon, 15 Feb 2010 07:26:43 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5492</guid>
		<description><![CDATA[With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries. Read and Write Collections to File NSArray *array = &#91;NSArray arrayWithObjects: @&#34;Hefeweizen&#34;, @&#34;IPA&#34;, @&#34;Pilsner&#34;, @&#34;Stout&#34;, nil&#93;; &#160; NSDictionary *dictionary = &#91;NSDictionary dictionaryWithObjectsAndKeys: array, @&#34;array&#34;, @&#34;Stout&#34;, @&#34;dark&#34;, @&#34;Hefeweizen&#34;, @&#34;wheat&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.</p>
<h5>Read and Write Collections to File</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Pilsner&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
  array, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dark&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wheat&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, 
  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hoppy&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get path to documents directory</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, 
  NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Path to save array data</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>arrayPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
      stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array.out&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Path to save dictionary</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>dictPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
      stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dict.out&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Write array</span>
  <span style="color: #002200;">&#91;</span>array writeToFile<span style="color: #002200;">:</span>arrayPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Write dictionary</span>
  <span style="color: #002200;">&#91;</span>dictionary writeToFile<span style="color: #002200;">:</span>dictPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Read both back in new collections</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfFile<span style="color: #002200;">:</span>arrayPath<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span>dictPath<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>element <span style="color: #a61390;">in</span> arrayFromFile<span style="color: #002200;">&#41;</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer: %@&quot;</span>, element<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dictFromFile<span style="color: #002200;">&#41;</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ Style: %@&quot;</span>, key, <span style="color: #002200;">&#91;</span>dictionary valueForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Verify the Output</h5>
<p>The output in the console window for the above looks as follows:<br />
<img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/screen.png" alt="" title="screen" width="512" height="227" class="alignnone size-full wp-image-5494" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading a plist into an NSArray</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html#comments</comments>
		<pubDate>Mon, 09 Nov 2009 07:08:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4498</guid>
		<description><![CDATA[Reading a plist into an NSArray This is the second tip in a series of four that is based on content from the O&#8217;Reilly book Head First iPhone Development. The first tip described how to adjust textfields that are hidden when the onscreen keyboard is shown. O’Reilly and iPhone Developer Tips are collaborating to give [...]]]></description>
			<content:encoded><![CDATA[<h5>Reading a plist into an NSArray</h5>
<p>This is the second tip in a series of four that is based on content from the O&#8217;Reilly book <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12">Head First iPhone Development</a>. The first tip described <a  target="_blank"  href="http://iphonedevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html">how to adjust textfields that are hidden when the onscreen keyboard is shown</a>.<br />
<span id="more-4498"></span></p>
<p><a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12"><img border="0" class="alignleft" style="margin: 5px;" src="http://iphonedevelopertips.com/images/ads/Book-Headfirst.jpg" alt="Head First iPhone Development" /></a>O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by referring a friend to this iPhone Developer tip – see the Refer-a-Friend button near the bottom of this post. You can read more about last weeks winner, Bill Mietelski, <a href="http://iphonedevelopertips.com/giveaway/book-giveaway-head-first-iphone-development-winner-announcement.html">on this post</a>.</p>
<p>This tip, based on content in Chapter 4 &#8211; Multiple View, shows how to read a plist from a file in the resource bundle, into an array.</p>
<h5>Hardcoded Array</h5>
<p>Based on the example in the book, let&#8217;s assume we are keeping a list of mixed drinks inside our application. Somewhere in the initialization process we might write something like the following to build an array of <strong>NSString</strong> values:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Firecracker&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Lemon Drop&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mojito&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show the string values  </span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> array<span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Creating a plist</h5>
<p>Although the code above works, a better solution would be to keep the list of values in a plist as part of the resource bundle, then read the plist into an array. </p>
<p>To create plist, in Xcode, right click on <strong>Resources</strong> then select <strong>Add</strong>, <strong>New File</strong>, <strong>Mac OS X</strong>, <strong>Resource</strong>, <strong>Property List</strong>. Name the file <strong>DrinkArray.plist</strong>. Change the root type to <strong>Array</strong> and then add the four string values shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/plist.png" /></p>
<h5>Reading a plist into an NSArray</h5>
<p>Reading the plist into an array is straight forward:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Path to the plist (in the application bundle)</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DrinkArray&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Build the array from the plist  </span>
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>array2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show the string values  </span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> array2<span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;--%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>plists and Other Data Types</h5>
<p>One of the benefits of working with plists, is that other types, for example <strong>NSDictionary</strong>, can be read from a plist. This means that you can create more complex data collections in a plist, such as an array of dictionaries.</p>
<p>For example, the plist below is also an array, however, the third value in the array is a dictionary. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/plist2.png" /></p>
<p>With plists you can build various data types needed by your application and read those values directly into your program.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>iPhone File System: Creating, Renaming and Deleting Files</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html#comments</comments>
		<pubDate>Tue, 27 Oct 2009 11:04:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4380</guid>
		<description><![CDATA[The iPhone provides a comprehensive set of operations for working with files and directories. NSFileManager includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc). Point to the Documents Directory Each application has its own sandbox in which you can read/write files. Files [...]]]></description>
			<content:encoded><![CDATA[<p>The iPhone provides a comprehensive set of operations for working with files and directories. <strong>NSFileManager</strong> includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc).</p>
<h5>Point to the Documents Directory</h5>
<p>Each application has its own sandbox in which you can read/write files. Files written to the sandbox are persistent across invocations of the application, including across application updates.</p>
<p>You can locate the Documents directory in the sandbox as shown below:<br />
<span id="more-4380"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// For error information</span>
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create file manager</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Point to Document directory</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Creating a File</h5>
<p>With the documents directory available, we can now use that path to create a new file in the sandbox and write a few lines of text:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// File we want to create in the documents directory </span>
<span style="color: #11740a; font-style: italic;">// Result is: /Documents/file1.txt</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file1.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// String to write</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iPhone Developer Tips<span style="color: #2400d9;">\n</span>http://iPhoneDevelopTips,com&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write the file</span>
<span style="color: #002200;">&#91;</span>str writeToFile<span style="color: #002200;">:</span>filePath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> 
         encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>We build a path to the file we want to create (file1.txt), initialize a string to write into the file, and write out the contents. The last line shows a directory listing of what is in the Documents directory after we create the file, see the figure below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr1.png" /></p>
<h5>Renaming a File</h5>
<p>To rename a file we move the file to a new path. The code below creates the destination path we are after, requests to move the file, and shows the Documents directory after the move.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Rename the file, by moving the file</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
                                 stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file2.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Attempt the move</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr moveItemAtPath<span style="color: #002200;">:</span>filePath toPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to move file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>, 
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>After moving the file, the output should look similar to the image below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr2.png" /></p>
<h5>Deleting a File</h5>
<p>To round out this tip, let&#8217;s look at how to delete a file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Attempt to delete the file at filePath2</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr removeItemAtPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to delete file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Once the file is deleted, as expected, the Documents directory is now empty:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr3.png" /></p>
<p>These examples touch the surface of working with files. Check out the documentation for <strong>NSFileManager</strong> for all the specifics.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Get Users Home Directory &#8211; Part 2</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html#comments</comments>
		<pubDate>Mon, 26 Jan 2009 04:47:22 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[home directory]]></category>
		<category><![CDATA[plist]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1469</guid>
		<description><![CDATA[In the previous tip on working with plists, I showed an example of how to create a path to a file to be stored in the /Documents folder. In this tip I want to show an example that arrives at the same end result, however, I&#8217;ll use methods which will eliminate the hardcoding of paths [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://iphonedevelopertips.com/data-file-management/get-users-home-directory.html">previous tip</a> on working with plists, I showed an example of how to create a path to a file to be stored in the <em>/Documents</em> folder.</p>
<p>In this tip I want to show an example that arrives at the same end result, however, I&#8217;ll use methods which will eliminate the hardcoding of paths as shown in the first example.<br />
<span id="more-1469"></span></p>
<p>To quickly recap, the intention was to create a file named <em>User.plist</em> and store that file in the user&#8217;s documents folder. In the previous post I specified the full file path I was after (/Documents/User.plist) and then appended that path to the user&#8217;s home directory. </p>
<p>Here&#8217;s another approach:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, 
    NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>plistPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
                                 stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Check for existence of the file</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>plistPath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    ... 
  <span style="color: #a61390;">else</span>
    ...
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>With this approach I request the path to the documents directory through a call to <em>NSSearchPathForDirectoriesInDomains</em> by specifying <em>NSDocumentDirectory</em> as the search path. To build the fullpath I first retrieve the path to the documents directory ([paths objectAtIndex:0]) and append the filename that I am after through a call to <em>stringByAppendingPathComponent</em>. The later call will prepend a path separator and append the filename requested. </p>
<p>The end result, with no hardcoding: /Documents/User.plist</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Users Home Directory &#8211; Part 1</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html#comments</comments>
		<pubDate>Fri, 23 Jan 2009 14:45:03 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[home directory]]></category>
		<category><![CDATA[plist]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1455</guid>
		<description><![CDATA[In a recent application I needed to create several property list files (plists) to store application information. I opted to store the files in the folder named &#8220;Documents&#8221; off the home folder. Here is the code I used to specify the path where I wanted the files written and the code to build an NSString [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent application I needed to create several property list files (plists) to store application information. I opted to store the files in the folder named &#8220;Documents&#8221; off the home folder.</p>
<p>Here is the code I used to specify the path where I wanted the files written and the code to build an NSString with the full path.<br />
<span id="more-1455"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Folder and file name</span>
<span style="color: #6e371a;">#define USER_PLIST_FULLPATH @&quot;/Documents/User.plist&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Path to the user property list</span>
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>plistPath;
plistPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingString<span style="color: #002200;">:</span>USER_PLIST_FULLPATH<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Check for existence of the file</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>plistPath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  ...
<span style="color: #a61390;">else</span>
  ...</pre></td></tr></table></div>

<p>The key to making this work is NSHomeDirectory() a low level C function that returns the path to the current user’s home directory. See the document <em>Low-Level File Management Programming Topics</em> for the specifics on working with the file system.</p>
<p>In the next post I&#8217;ll show another means to generate paths to files, arguably a better approach as it uses the provided frameworks for building paths (versus hardcoding as shown here).<br />
<strong><br />
Important Note:</strong> There is a <em>/Documents</em> directory created in the sandbox for each iPhone application. In other words, the plist file that I created above will not clash with a file of the same name in another iPhone application. </p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

