开发者

Location of my own directories

I am trying to und开发者_Go百科erstand 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.


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"];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜