【问题标题】:Prevent app of iCloud Backup阻止 iCloud 备份应用
【发布时间】:2014-08-29 12:23:45
【问题描述】:

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

我在应用委托中有以下代码:

-(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 中的构建文件夹,在我的 iPad Air 7.1.2 上删除我的应用程序

(已启用 iCloud 备份)

构建应用程序后,我导航到设置 > iCloud > 存储和备份 > 管理存储 > 我的 iPad > 显示所有应用程序,就可以了!!我的应用程序在列表中,大小为 4.2 MB。

当我导航到“设置”>“常规”>“使用”>“显示所有应用”时,我的应用大约有 11 MB 大。

我问自己我的解决方案现在是否会通过 Apple 审核流程? (除了没有其他不好的东西)

【问题讨论】:

    标签: ios backup icloud documents


    【解决方案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]) {
    ...
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        • 2015-01-14
        • 2012-10-12
        • 2016-05-27
        相关资源
        最近更新 更多