防止iCloud备份的应用程序

时间:2014-07-08 16:47:16

标签: ios backup icloud documents

我的应用程序从App Store Review中恢复,因为我没有阻止我的数据来自iCloud备份:/

我在app delegate中有以下代码:

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL {
if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
    NSLog(@"File %@ doesn't exist!",[fileURL path]);
    return NO;
}

NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer isEqualToString:@"5.0.1"]) {
    const char* filePath = [[fileURL path] fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    NSLog(@"Excluded '%@' from backup",fileURL);
    return result == 0;
}
else if (&NSURLIsExcludedFromBackupKey) { //iOS 5.1 and later
    NSError *error = nil;
    BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (result == NO) {
        NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error);
        return NO;
    }
    else { // Succeeded
        NSLog(@"Excluded '%@' from backup",fileURL);
        return YES;
    }
} else {
    // iOS version is below 5.0, no need to do anything because there will be no iCloud present
    return YES;
}
}

// now call  above method from where you are storing your files/folders in NSDocumnetsDirectory
-(BOOL) doNotBackUpMyFolderOrFile {

NSString *applicationCacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                     NSUserDomainMask,
                                                                     YES) lastObject];

NSURL *pathURL= [NSURL fileURLWithPath: applicationCacheDir isDirectory:YES];


NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = [paths lastObject];


NSURL *tmpURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

if ([self addSkipBackupAttributeToItemAtURL:documentsURL] &&
    [self addSkipBackupAttributeToItemAtURL:pathURL]      &&
    [self addSkipBackupAttributeToItemAtURL:tmpURL] ) {
    return YES;
}
return NO;
}

我致电[self doNotBackUpMyFolderOrFile]-application:didFinishLaunchingWithOptions:

当我打开终端并导航到

/ Library / Application Support / iPhone Simulator / 7.1 / Applications / 9224585D-9B05-44E5-8A84-1AEFB56E4579

并输入ls -al -@

终端打印

total 176
drwxr-xr-x@  8 user  staff    272 Jul  8 16:18 .
com.apple.installd.container_bundle_id     35
com.apple.installd.container_creation_os_build      7
drwxr-xr-x  13 user  staff    442 Jul  8 17:42 ..
-rw-r--r--@  1 user  staff   6148 Jul  8 16:53 .DS_Store
com.apple.FinderInfo       32
drwxr-xr-x@  7 user  staff    238 Jun 17 13:03 Documents
com.apple.metadata:com_apple_backup_excludeItem    22
drwxr-xr-x   6 user  staff    204 Jul  8 16:53 Library
drwxr-xr-x  59 user  staff   2006 Jul  8 16:24 APPNAME.app
-rw-r--r--   1 user  staff  80109 Jun 17 11:24 iTunesArtwork
drwxr-xr-x   2 user  staff     68 Jul  8 16:22 tmp
  
    
      

所以有Documents com.apple.metadata:com_apple_backup_excludeItem

    
  

现在!我正在清理Xcode中的Build Folder,在iPad Air 7.1.2上删除我的应用程序

(已启用iCloud备份)

构建应用后,我导航到设置> iCloud>存储&备份>管理存储>我的iPad>显示所有应用程序,它有它!我的应用程序在4.2 MB的列表中。

当我导航到设置>一般>用法>显示所有应用程序我的应用程序大约是11 MB。

我问自己,我的解决方案现在是否会通过Apple Review Process? (除了没有其他事情是坏的)

2 个答案:

答案 0 :(得分:1)

这是我的解决方案。非常硬编码,但它有效。

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL {
if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
    NSLog(@"File %@ doesn't exist!",[fileURL path]);
    return NO;
}

NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer isEqualToString:@"5.0.1"]) {
    const char* filePath = [[fileURL path] fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    NSLog(@"Excluded '%@' from backup",fileURL);
    return result == 0;
}
else if (&NSURLIsExcludedFromBackupKey) { //iOS 5.1 and later
    NSError *error = nil;
    BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (result == NO) {
        NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error);
        return NO;
    }
    else { // Succeeded
        NSLog(@"Excluded '%@' from backup",fileURL);
        return YES;
    }
} else {
    // iOS version is below 5.0, no need to do anything because there will be no iCloud present
    return YES;
}
}

// now call  above method from where you are storing your files/folders in NSDocumnetsDirectory
-(BOOL) doNotBackUpMyFolderOrFile {

NSString *applicationCacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                     NSUserDomainMask,
                                                                     YES) lastObject];

NSURL *pathURL= [NSURL fileURLWithPath: applicationCacheDir isDirectory:YES];


NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = [paths lastObject];


NSURL *tmpURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

NSString* libpath = [NSSearchPathForDirectoriesInDomains(
                                                      NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *libURL= [NSURL fileURLWithPath: libpath isDirectory:YES];

if ([self addSkipBackupAttributeToItemAtURL:documentsURL] &&
    [self addSkipBackupAttributeToItemAtURL:pathURL]      &&
    [self addSkipBackupAttributeToItemAtURL:tmpURL]       &&
    [self addSkipBackupAttributeToItemAtURL:libURL]) {
    return YES;
}
return NO;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

if ([self doNotBackUpMyFolderOrFile]) {
...

答案 1 :(得分:0)

这个问题现在有点陈旧,但我遇到了同样的问题:我实施了addSkipBackupAttribute并将数据移到缓存目录中,但是当我检查时在iCloud备份设置上,我的应用程序仍有7MB的备份。

这是我的解决方案:我的所有图形资源(图像等等大约7MB)都放在我项目的支持文件文件夹中。 我将这些资源移到我的主项目文件夹(或项目的任何其他经典子文件夹)中,并且tadaa:我的iCloud app备份现在是14kb:)

显然,所有支持文件资源都会自动保存在应用备份中......