iTunes rejected App because of 2.23: Apps must follow the iOS Data Storage Guidelines or they will be rejected

Recently while updating next version of my client app on iTunes store i got rejected and the reason mentioned in resolution centre was


2.23: Apps must follow the iOS Data Storage Guidelines or They Will be rejected 
I did not understand why they reject it now as i only made a change in web service and nothing else. I made a quick google search about this and found a super answer on http://stackoverflow.com and I followed the same steps to solve rejection issue and thought of it sharing here on this blog. 

According to our normal development approach we save images and other stuff like database in Document directory. By default IOS copy all the files from Document directory to iCloud if it is turned on. So if your document directory data size is more (in few MB’s) then the limit you will get Apple rejection for not following IOS data storage guidelines. To avoid rejection you can mark your directory or file to not be backed up by iCloud.  Below is the code, using which you can prevent files or directories to be copied or backed up by iCloud.


 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
 NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
 if(documentPath!=nil)
    {
        NSURL *url = [NSURL fileURLWithPath:documentPath isDirectory:YES];
        NSError *error = nil;
        BOOL success = [url setResourceValue: [NSNumber numberWithBool: YES]
                                      forKey: NSURLIsExcludedFromBackupKey error: &error];
        if(!success){
            NSLog(@”Error excluding %@ from backup %@”, [url lastPathComponent], error);
        }
    }
In the above code, i marked my document directory to not be backed up iCloud. You can mark a file or subdirectories also to not be backed up by iCloud .
For file you can use below code
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *docDirectory = [paths objectAtIndex:0];
 NSString *exportPath = docDirectory;
 longPath = [exportPath stringByAppendingPathComponent:path];
 if(longPath!=nil)
 {
    NSURL *url = [NSURL fileURLWithPath:longPath];
    NSError *error = nil;
    BOOL success = [url setResourceValue: [NSNumber numberWithBool: YES]
    forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success)
    {
         NSLog(@”Error excluding %@ from backup %@”, [url lastPathComponent], error);
    }
 }