0

I would like to have an images folder created inside of the documents folder of my app to house a bunch of images I download at a later point. How do I create this folder when the app is first installed? I am sure it's a setting in Xcode that I just cannot find...

PretzelJesus
  • 859
  • 3
  • 11
  • 20
  • 1
    Have you tried searching? – trojanfoe Jul 08 '14 at 14:30
  • There're no settings for this. You have to create one in code, using `NSFileManager` – Alessandro Vendruscolo Jul 08 '14 at 14:31
  • possible duplicate of [Create a folder inside documents folder in iOS apps](http://stackoverflow.com/questions/1762836/create-a-folder-inside-documents-folder-in-ios-apps) – danh Jul 08 '14 at 14:31
  • trojanfoe : +1 Maybe you looking for something like FileSharing. If you activate this, you could be able to throw you files and folder directly to your app directory via iTunes. – Watsche Jul 08 '14 at 14:33

1 Answers1

1

This is how I create my directories on the first run of the app, placed in AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
  (NSDictionary*)launchOptions {

/////////////////////////////////////////
// Create Content directories if needed
/////////////////////////////////////////

NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir;
NSString *newDir;
NSString *thumbNails;
NSString *largeImages;

filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                               NSUserDomainMask, YES);
docsDir = dirPaths[0];

// Picture Directory
newDir = [docsDir stringByAppendingPathComponent:@"/myPictures"];
if(![filemgr fileExistsAtPath:newDir])
{
    NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/myPictures"]];
    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:[docsDir stringByAppendingPathComponent:@"/myPictures"] withIntermediateDirectories:YES attributes:nil error:&error];
    [data writeToFile:newDir atomically:YES];
}

// Nested Thumbnail Directory
thumbNails =[docsDir stringByAppendingPathComponent:@"/myPictures/thumb"];
if(![filemgr fileExistsAtPath:thumbNails])
{
    NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/myPictures/thumb"]];
    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:[docsDir stringByAppendingPathComponent:@"/myPictures/thumb"] withIntermediateDirectories:YES attributes:nil error:&error];
    [data writeToFile:thumbNails atomically:YES];
}
  • While this is not a particularly expensive process, it's always best to defer this kind of processing until it's needed. I.E., perform this action when the first file needs to be stored. – CuriousRabbit Jul 08 '14 at 15:23