Ivan Titkov Ivan Titkov - 1 year ago 65
iOS Question

How to hide folder in NSDocumentsDirectory and disallow backup via iTunes & iCloud

I create private folder in NSDocumentDirectory and i want to hide it in iTunes and disallow to backup.

In this question How to hide folders created in Document Directory in ios? people suggest to save in private directory.

But it's not ok according apple documentation https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Application_Home/Library/ You should not use this directory for user data files.

Before iTunes11 this solution was work for me

Can I add the Do not Back up for the "Document Directory" for iCloud

but now i see folder with private docs in iTunes.

Could somebody help me?

Answer Source

For hiding your app from iTunes file sharing, you can set the below key to No in your info.plist.

Application supports iTunes file sharing

Alternatively, prepend your filenames / directories added the the Documents directory with a . to hide it without disabling iTunes File Sharing. E.g. .folderName.

Use this to prevent iCloud backup, from Prevent Backup to iCloud,is following code correct?

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *)filePathString {
    NSURL *fileURL = [NSURL fileURLWithPath:filePathString];

    assert([[NSFileManager defaultManager] fileExistsAtPath: [fileURL path]]);

    NSError *error = nil;

    BOOL success = [fileURL setResourceValue:[NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey
                                 error: &error];
    return success;

And to prevent app backup from XCode Organizer when the device is locked, use this snippet

// Method for making files and folders secure
+ (void)makeItemAtPathSecure:(NSString *)path
    NSError *securingFilesError;

    NSFileManager *manager=[NSFileManager defaultManager];

    NSDictionary *attrs = [manager attributesOfItemAtPath:path error:&securingFilesError];

    if(![[attrs objectForKey:NSFileProtectionKey] isEqual:NSFileProtectionComplete])

        if(![manager setAttributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete                                                                       forKey:NSFileProtectionKey] ofItemAtPath:path error:&securingFilesError])
            NSLog(@"Problem in securing files: %@",[securingFilesError localizedDescription]);

        NSLog(@"Problem in securing files: %@",[securingFilesError localizedDescription]);



For selectively hiding folders in iTunes try renaming the folder with a . before it, like folderName should be .folderName

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download