15

My application got rejected by Apple due to :

2.23: Apps must follow the iOS Data Storage Guidelines or they will be rejected

so is it possible that flag entire Document directory as do no backup ? my application runs in iOS 6.1 and higher .

I am familiar with this code , but I don't know hot to mark my Document directory with it

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

EDITED :

Apple has told me that iCloud backups my photos from image gallery , I load images of gallery like this :

- (void)setupPageScroll {

    NSString *imgCount;
    int i;

    [_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width * _pageControl.numberOfPages, _scrollView.frame.size.height)];
    _scrollView.delegate = self;

    for (i = 0 ; i < 10 ; i++) {

          imgCount  = [NSString stringWithFormat:@"%@%d.jpg",_gallName , i];
         [self createPageWithImage:[UIImage imageNamed:imgCount] forPage:i];
    }

}




- (void)createPageWithImage:(UIImage *)images forPage:(int)page
{
    UIView *newView = [[UIView alloc] initWithFrame: CGRectMake(_scrollView.frame.size.width * page, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];


    UIImageView *tempImage = [[UIImageView alloc]initWithImage:images];
    [tempImage setContentMode:UIViewContentModeScaleAspectFit];
    tempImage.frame = _galleryView.frame;
    _imgGallery = tempImage;
    [newView addSubview: _imgGallery];

    [_scrollView addSubview: newView];

}



- (IBAction)pageChanged:(id)sender {

    CGRect pageRect = CGRectMake(_pageControl.currentPage * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
    [_scrollView scrollRectToVisible: pageRect animated: YES];

}

How can skip these images ?

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
  • What is the value of the *_gallName* in ``- (void)setupPageScroll``? It seems that you have some bug earlier in the code. – Wojciech Rutkowski Nov 14 '13 at 12:53
  • @WojtekRutkowski no it's not bug ! _gallName is a NSString . my application is about solar system , when user select a planet this string change to planet's photo names . – iOS.Lover Nov 14 '13 at 13:42
  • By bug I meant something wrong according to iOS Data Storage Guidelines. I've asked about value not type, I can see that it is NSString. Can you insert here the real value of the *_gallName*? – Wojciech Rutkowski Nov 14 '13 at 14:33
  • I would forget about the "do not back up" attribute completely. What files are you creating while you app is running? And, for what purpose? – Thomas Nov 22 '13 at 16:27

2 Answers2

23

... And finally my application has been approved . just follow this instruction :

put this code on appDelegate.m (didFinishLunching)

NSString *docsDir;
NSArray *dirPaths;
NSURL * finalURL;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];


finalURL = [NSURL fileURLWithPath:docsDir];


[self addSkipBackupAttributeToItemAtURL:finalURL];

and skip backup :

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

{

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



    NSError *error = nil;

    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]

                                  forKey: NSURLIsExcludedFromBackupKey error: &error];

    if(!success){

        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);

    }

    return success;
}
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
  • 1
    I wouldn't suggest this method since Apple says: "You can use this property to exclude cache and other app support files which are not needed in a backup. Some operations commonly made to user documents cause this property to be reset to false; consequently, do not use this property on user documents". Reference: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/index.html#//apple_ref/c/data/NSURLIsExcludedFromBackupKey – Bruno Koga Jul 18 '16 at 13:03
9

You can mark as no-backup only the parent folder, not every file. But it should not be a Documents directory as it should contain only user-created content. You should use Library/Application Support (Technical Q&A QA1719)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
directory = [directory stringByAppendingPathComponent:@"MyData"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:directory] == NO) {

    NSError *error;
    if ([fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error] == NO) {
        NSLog(@"Error: Unable to create directory: %@", error);
    }

    NSURL *url = [NSURL fileURLWithPath:directory];
    // exclude downloads from iCloud backup
    if ([url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error] == NO) {
        NSLog(@"Error: Unable to exclude directory from backup: %@", error);
    }
}

Credits: xinsight blog

Wojciech Rutkowski
  • 11,299
  • 2
  • 18
  • 22