iOS - 将整个文档目录标记为不备份

时间:2013-11-13 21:33:07

标签: ios nsfilemanager

由于以下原因,我的申请被Apple拒绝了:

  

2.23:应用必须遵循iOS数据存储指南,否则将被拒绝

所以有可能将整个Document目录标记为不备份吗?我的应用程序在iOS 6.1及更高版本中运行。

我熟悉这段代码,但我不知道用它来标记我的Document目录

- (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;
}

已编辑:

Apple告诉我iCloud从图片库备份我的照片,我加载了这样的图片库:

- (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];

}

如何跳过这些图片?

2 个答案:

答案 0 :(得分:23)

......最后我的申请获得批准。按照这个指示:

将此代码放在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];

并跳过备份:

- (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;
}

答案 1 :(得分:9)

您可以仅将父文件夹标记为无备份,而不是每个文件。但它不应该是Documents目录,因为它应该只包含用户创建的内容。您应该使用图书馆/应用支持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);
    }
}

致谢:xinsight blog