阻止应用备份文档文件夹? [英] Prevent app from backing up documents folder?

查看:146
本文介绍了阻止应用备份文档文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阻止我的应用程序将文件备份到iCloud,但已经完全混淆并且有点丢失。

I'm trying to prevent my app backing up files to iCloud but have become completely confused and a little lost.

-EDIT -

-EDIT-

我已经对此进行了更新以反映我在下面的海报上所做的更改。

I've updated this to reflect the changes I've made thanks to the posters below.

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

I want to prevent back up of files which are downloaded to the app's documents directory.

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

So far I have a class called PreventBackup with the following method:

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

我在应用程序启动时使用此代码调用它:

I'm then calling it with this code when the app starts:

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

cosole打印防止调用无错误的备份方法但该应用程序仍然显示具有相同数量的数据用于备份之前。

The cosole prints prevent backup method called without error but the app is still showing as having the same amount of data for back up with it did before.

任何想法出错的地方?

-EDIT 2 -

-EDIT 2-

好的,我认为这已经解决了。文件正在下载到名为downloads的子文件夹。更改上面的代码,使其读取如下似乎已成功:

OK, I think this is solved. The files are downloading to a sub folder called "downloads". changing the code above so that it reads as follows appears to have done the trick:

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

感谢您的帮助。

推荐答案

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

这篇关于阻止应用备份文档文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆