{"id":1313,"date":"2012-06-30T15:57:52","date_gmt":"2012-06-30T22:57:52","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1313"},"modified":"2015-04-07T08:49:25","modified_gmt":"2015-04-07T15:49:25","slug":"finding-directories-in-ios","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1313","title":{"rendered":"Finding Directories in iOS"},"content":{"rendered":"<p>I created a Utilities singleton to keep things I refer to often. These are the methods I use for referring to directories.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n#pragma mark - Application&#039;s Documents directory\n\/\/ Directory locations\n+ (NSString *)applicationCachesDirectory {\n&nbsp;&nbsp;&nbsp;&nbsp;return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];\n}\n\n+ (NSString *)applicationDocumentsDirectory {\n&nbsp;&nbsp;&nbsp;&nbsp;return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];\n}\n\n+ (NSString *)applicationLibraryDirectory {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];\n}\n<\/code><\/pre><\/p>\n<p>Here&#8217;s one for retrieving the contents of a file stored in the Caches Directory. Notice how it uses [self applicationCachesDirectory] to get the location of the directory.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ Cached files for results\n+ (NSString *)cachedFilePath:(NSString *)fileName {\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *pathComponent = [NSString stringWithFormat:@&quot;%@.txt&quot;,fileName];\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *filePath = [[self applicationCachesDirectory] stringByAppendingPathComponent:pathComponent];\n&nbsp;&nbsp;&nbsp;&nbsp;return filePath;\n}\n<\/code><\/pre><\/p>\n<p>If I want to store the results in the Documents directory, I&#8217;ll use this to construct the path.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSString *resultsFilePath = [ [Utilities applicationDocumentsDirectory] stringByAppendingPathComponent:[self formattedHTMLFileName:@&quot;Results&quot;] ];\n<\/code><\/pre><\/p>\n<p>Here\u2019s a slightly more complicated example where I retrieve the contents of a cached file. Note that it makes use of <i>cachedFilePath:fileName<\/i> as described above.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n+ (NSString *)cachedFileContents:(NSString *)fileName {\n&nbsp;&nbsp;&nbsp;&nbsp;NSStringEncoding encoding; NSError* error = nil;\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *text = [NSString stringWithContentsOfFile:[self cachedFilePath:fileName] usedEncoding:&amp;encoding error:&amp;error];\n&nbsp;&nbsp;&nbsp;&nbsp;return text;\n}\n<\/code><\/pre><\/p>\n<p>Here\u2019s another example. This time I\u2019m loading a string with the contents of the FullResults file.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n NSStringEncoding encoding;\n&nbsp;&nbsp;&nbsp;&nbsp;NSError* error = nil;\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *resultsText = [NSString stringWithContentsOfFile:[Utilities cachedFilePath:@&quot;FullResults&quot;] usedEncoding:&amp;encoding error:&amp;error];\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I created a Utilities singleton to keep things I refer to often. These are the methods I use for referring to directories. #pragma mark &#8211; Application&#039;s Documents directory \/\/ Directory locations + (NSString *)applicationCachesDirectory { &nbsp;&nbsp;&nbsp;&nbsp;return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; } + (NSString *)applicationDocumentsDirectory { &nbsp;&nbsp;&nbsp;&nbsp;return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } + (NSString *)applicationLibraryDirectory { &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1313\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Finding Directories in iOS<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1313","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1313"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}