4

我最近的 iOS 应用程序刚刚被拒绝,因为该应用程序将数据存储在 Documents 上,因此它由 iCloud 备份,这是不允许的,因为数据是从服务器下载的。

我现在正在尝试重建代码,并且在将数据存储在名为 Application Support 的文件夹中时遇到了困难。

但即使我使用 addSkipBackupAttributeToItemAtURL 它仍然显示为 iCloud 的备份。

我只针对 5.1,所以这不是版本问题。

我在下面添加了受影响的代码:

    NSString *newPath = [NSMutableString stringWithFormat:@"%@/Library/Application Support/", NSHomeDirectory()];

    if (![[NSFileManager defaultManager] fileExistsAtPath:newPath]) //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:newPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *guidesPath = [newPath stringByAppendingPathComponent:@"/Guides"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:guidesPath])  //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:guidesPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *dataPath = [guidesPath stringByAppendingPathComponent:dbName];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *newGuidesPath = [dataPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *guidesURL = [NSURL URLWithString:newGuidesPath];
    [self addSkipBackupAttributeToItemAtURL:guidesURL];

    NSString* path = [dataPath stringByAppendingPathComponent: 
                      [NSString stringWithString: finalName] ];
    if (![fileManager fileExistsAtPath:path]) {
        [myData writeToFile:path atomically:YES];
        NSString *docFile = [dataPath stringByAppendingPathComponent: @"guideid.txt"];
        [[guideID dataUsingEncoding:NSUTF8StringEncoding] writeToFile:docFile atomically:NO];
        DLog(@"Saved database here:%@", path);
    }else{
        DLog(@"Already exists, no need to copy");
    }

}

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

2 回答 2

9

实际上找到了一个解决方案,我已经对不需要的 URL 进行了编码。刚刚添加到文件夹

    NSURL *guidesURL = [NSURL fileURLWithPath:guidesPath];
    [guidesURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:NULL];

并跳过了排除方法,只是在代码中标记了它,这似乎有效。

于 2012-05-28T14:38:54.730 回答
0

您是否尝试从 icloud 设置在您的设备上创建新备份?

于 2012-05-28T14:32:49.070 回答