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



我的申请被苹果拒绝了,原因是:

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

那么是否有可能将整个文档目录标记为不备份? 我的应用程序在iOS 6.1及更高版本中运行。

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

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

编辑:

苹果告诉我,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];
}

如何跳过这些图像?

...最后我的申请得到了批准.只需按照以下说明操作:

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

您只能将父文件夹标记为无备份,而不能将每个文件标记为无备份。但它不应该是一个文档目录,因为它应该只包含用户创建的内容。您应该使用库/应用程序支持(技术问答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 博客

最新更新