【问题标题】:Prevent app from backing up documents folder?防止应用程序备份文档文件夹?
【发布时间】:2012-11-20 21:29:49
【问题描述】:

我正试图阻止我的应用程序将文件备份到 iCloud,但我完全感到困惑并且有点迷失。

-编辑-

感谢下面的海报,我已更新此内容以反映我所做的更改。

我想防止备份下载到应用程序文档目录的文件。

到目前为止,我有一个名为 PreventBackup 的类,方法如下:

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

然后我在应用启动时使用此代码调用它:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];

cosole 打印出prevent backup method called without error,但该应用程序仍显示为具有与以前相同的数据量进行备份。

知道这是哪里出了问题吗?

-编辑 2-

好的,我想这已经解决了。这些文件正在下载到名为“downloads”的子文件夹中。将上面的代码更改为如下所示似乎已经成功了:

NSString *downloadsFolder = [documentsDirectory stringByAppendingPathComponent:(@"/downloads")];
NSURL *pathURL= [NSURL fileURLWithPath:downloadsFolder];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];

感谢大家的帮助。

【问题讨论】:

标签: objective-c ios xcode backup icloud


【解决方案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;
}

NSURL *documentURLWithExtension = [documentURL URLByAppendingPathExtension:extensionType];

将这个“documentURLWithExtension”传递给这个函数

[self addSkipBackupAttributeToItemAtURL:documentURLWithExtension];

【讨论】:

  • 非常感谢。这似乎有点用处。我需要注释掉assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
【解决方案2】:

在 Swift 中:

//Path of document directory
var docPathAry : NSArray! = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var docDirPathStr: AnyObject? = docPathAry.count > 0 ? docPathAry[0] : nil

self.addSkipBackupAttributeToItemAtURL(NSURL.fileURLWithPath(docDirPathStr as NSString))

和:

func addSkipBackupAttributeToItemAtURL(URL: NSURL!) -> Bool {
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path))
    var err : NSError? = nil
    var success : Bool! = URL.setResourceValue(NSNumber.numberWithBool(true), forKey: NSURLIsExcludedFromBackupKey, error: &err)

    if(!success) {
        println("Error excluding \(URL.lastPathComponent) from backup\(err) ")
    }

    return success
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2011-04-13
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多