7

I have created some PDF files programatically, which i am storing into the devices memory using the following code >>>>

    NSString *fileName = [NSString stringWithFormat:@"SampleTextFile.pdf",strFinalString];

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *saveDirectory = [path objectAtIndex:0];
    NSString *saveFileName = fileName;
    NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];

I can see the file in the Devices Document folder.

I want to hide these files so that the user can not see or delete it.

Can anyone help me out to do this.

iLearner
  • 1,670
  • 2
  • 19
  • 45
  • why? normal user won't be able to see them anyway – Bryan Chen Feb 09 '12 at 08:29
  • Maybe they mean from within iTunes? – twilson Feb 09 '12 at 08:31
  • Yeah, if you set the document sharing flag in the info.plist, users can see any files in the Documents folder in iTunes. Generally you shouldn't store private data files in Documents, that should only be used for user documents (see below for alternative locations to store application data). – Nick Lockwood Feb 09 '12 at 09:20

2 Answers2

13

A good place to store private data is in ~/Library/Application Support/, which is the folder used on the Mac for this purpose.

You can generate a path to this folder using:

NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];

You'll have to create the folder yourself the first time you use it, which you can do with:

if (![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:NULL];
}

I wrote a simple library that makes this and all other useful iOS folders available as methods on NSFileManager: https://github.com/nicklockwood/StandardPaths

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • +1, useful category (and nicely documented). One request, though: it would be nice if you added comments in the header file as well about what each method does. If someone adds your category to his project and his colleague sees it he might not know what the methods are supposed to return or do. – DarkDust Feb 09 '12 at 09:14
  • Valid point. I generally like to leave my header files uncluttered if possible, and there is a link to the github documentation in the .h file, but I can see an argument for putting docs in the header. – Nick Lockwood Feb 09 '12 at 09:21
  • Is this path private, or can multiple applications read from this folder? – f2prateek Jan 12 '18 at 01:45
  • @f2prateek it's inside the app sandbox, so it's private. – Nick Lockwood Mar 09 '18 at 13:38
4

Just prefix the filename with a dot, as in .SampleTextFile.pdf.

But the real solution is to not store the document in the NSDocumentDirectory in the first place. You should create subdirectory in the NSLibraryDirectory and store this stuff there. It also gets backed up and will not get purged like Caches and tmp, but the user cannot access it with iTunes.

DarkDust
  • 90,870
  • 19
  • 190
  • 224