{"id":1318,"date":"2012-07-04T07:52:15","date_gmt":"2012-07-04T14:52:15","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1318"},"modified":"2015-04-07T08:47:32","modified_gmt":"2015-04-07T15:47:32","slug":"copy-a-file-to-the-documents-directory","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1318","title":{"rendered":"Copy a file to the Documents directory in iOS"},"content":{"rendered":"<p>This is a simple method that I use to copy a file from the app bundle to the Documents directory. You can\u2019t modify files in the app bundle, but you can modify them if they are in the Documents directory. <\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n- (void)createResultsDocument {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *documentsDirectory = [self applicationDocumentsDirectory];\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@&quot;HTML_header.html&quot;];\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];\n&nbsp;&nbsp;&nbsp;&nbsp;[dateFormatter setDateFormat:@&quot;yyyy-MM-dd&quot;];\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *resultsFileName = [NSString stringWithFormat:@&quot;MPResults_%@.html&quot;,todaysDate];\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:resultsFileName];\n&nbsp;&nbsp;&nbsp;&nbsp;NSError *error;\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;[[NSFileManager defaultManager] copyItemAtPath:sourcePath \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toPath:destinationPath\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error:&amp;error];\n&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@&quot;The copy error is: %@&quot;, error);\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Check that it worked.\n&nbsp;&nbsp;&nbsp;&nbsp;NSURL *resultsURL = [NSURL fileURLWithPath:destinationPath];\n&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@&quot;The resultsURL is: %@&quot;, resultsURL);\n}\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple method that I use to copy a file from the app bundle to the Documents directory. You can\u2019t modify files in the app bundle, but you can modify them if they are in the Documents directory. &#8211; (void)createResultsDocument { &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;NSString *documentsDirectory = [self applicationDocumentsDirectory]; &nbsp;&nbsp;&nbsp;&nbsp;NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1318\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Copy a file to the Documents directory 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-1318","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1318","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=1318"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1318\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}