Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Flag entire Document directory as do not backup

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 ?

like image 581
iOS.Lover Avatar asked Nov 13 '13 21:11

iOS.Lover


2 Answers

... 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;
}
like image 60
iOS.Lover Avatar answered Oct 24 '22 04:10

iOS.Lover


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

like image 28
Wojciech Rutkowski Avatar answered Oct 24 '22 04:10

Wojciech Rutkowski