views:

98

answers:

2

Hello,

I am trying to understand how to create my own directory tree and access it from my application.

I am doing some kind of learning program and want to create a directory structure like this:

-MyLessonsDirectory
--LessonDirectory_1
---Step_1
---Step_2
---Step_3
--LessonDirectory_2
---Step_1
---Step_2
---Step_3
.
.
and so on

In each "Step"-directory I will have specific content for that step of the lesson. The content is added by me while developing. Hence, I don't want to create the structure and content from within the application, just access it and reading the content of the directories. If I can create this kind of structure and use it it will be very easy to create a nice design that makes it easy to create new lessons in my app.

What I cannot figure out is where I should create those directories in the file-structure in xCode and what is the path to those directories from my application.

Thanks in advance!

+2  A: 

You want to look at NSFileManager to create directories:

NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fm createDirectoryAtPath:@"path....." withIntermediateDirectories:YES attributes:nil error:&error];
if(result){
   NSLog(@"success!");
} else {
   NSLog(@"error: %@",error);
}

As far as where to create the directories, you can put it in ~/Application Support/--Your App's Name-- This is where you should store your app's data if the user doesn't require access to it. Otherwise, you should let the user decide where to save it.

- (NSString *)applicationSupportDirectory {

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
       NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
       return [basePath stringByAppendingPathComponent:@"MyApplication"];
}
christo16
Thank you for your answer. I think I wasn't clear in my question. What I want to do is to add content manually before I start the application. Then, from within the application I will read the content I added before starting the application. I have edited my question to reflect this.
Nicsoft
I did create a directory directly under the project in xCode, ran the application and then checked in the /Users/Niklas/Library/Application Support/iPhone Simulator/User/Applications and inside my app. The directory didn't come along so I guess it doesn't work like I want it, creating my own directory structure... Thanks anyway, I guess I have to find a different solution, perhaps naming of the files could be used to simulate a directory structure...hmmm...
Nicsoft
+1  A: 

On the iPhone, you only have access to the apps own directories. The best place to put practice directories in the Documents folder in the app directory.

This...

NSString *dp=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

... gives you the path to the directory folder which you can then hand off to the NSFileManager.

If you want to see the directory structure visually, run the app on the simulator and look in

~/Library/Application Support/iPhone Simulator/User/Applications

The app will be in one of the folders with the gibberish UUID name. You can see the directories and files there in the Finder.

I would add that the iPhone already has most of the directories you might practically need. You have preferences, cache, tmp, Documents etc all in place.

Edit01:

What I want to do is to add content manually before I start the application. Then, from within the application I will read the content I added before starting the application.

You cannot alter the default directory structure and you have absolutely no control over how the application or its directories are installed. iPhone apps do not have external support files of any kind. Every thing is inside the app bundle.

However, the app bundle is really just a directory so you can stuff things inside it and copy them out into one of the default directories the first time the app runs. You can write anything you want to the documents directory.

TechZen
Thank you for your answer. I think I wasn't clear in my question. What I want to do is to add content manually before I start the application. Then, from within the application I will read the content I added before starting the application. I have edited my question to reflect this. Reading your first sentence, does this mean that I cannot do what I want?
Nicsoft