【问题标题】:iOS - Flag entire Document directory as do not backupiOS - 将整个文档目录标记为不备份
【发布时间】:2013-11-26 16:57:26
【问题描述】:

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

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

那么是否有可能将整个 Document 目录标记为没有备份?我的应用程序在 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;
}

已编辑:

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

}

如何跳过这些图像?

【问题讨论】:

  • - (void)setupPageScroll_gallName 的值是多少?看来您的代码前面有一些错误。
  • @WojtekRutkowski 不,这不是错误! _gallName 是一个 NSString 。我的应用程序是关于太阳系的,当用户选择一个行星时,这个字符串会更改为行星的照片名称。
  • 根据 iOS 数据存储指南,我的意思是错误。我问过值不是类型,我可以看到它是 NSString。您能在此处插入 _gallName 的实际值吗?
  • 我会完全忘记“不备份”属性。当您的应用程序运行时,您正在创建哪些文件?还有,目的是什么?

标签: ios nsfilemanager


【解决方案1】:

您可以仅将父文件夹标记为无备份,而不是每个文件。但它不应该是 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

【讨论】:

【解决方案2】:

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

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

【讨论】:

  • 我不建议使用此方法,因为 Apple 表示:“您可以使用此属性排除备份中不需要的缓存和其他应用程序支持文件。通常对用户文档进行的一些操作会导致此问题属性重置为 false;因此,请勿在用户文档上使用此属性”。参考:developer.apple.com/library/ios/documentation/Cocoa/Reference/…
猜你喜欢
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
相关资源
最近更新 更多