我的应用程序刚被Apple拒绝,因为我将资源存储在Documents文件夹下! Documents文件夹自动同步到iCloude,因此只有用户生成的数据才能存储在Documents下。所有应用程序数据都应在Application bundle下。
我在我的项目中使用以下方法
- (NSString *)filePath:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
return path;
}
例如,以下行解压缩文档文件夹下的资源。
[SSZipArchive unzipFileAtPath:zipFileName toDestination:[self filePath:@“”]];
如何将这些文件和资源文件夹图像移动到应用程序包中并访问它们?
[编辑]
- (NSString *)filePath:(NSString *)fileName {
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/DoNotBackUp"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
// Set do not backup attribute to whole folder
if (iOS5) {
BOOL success = [self addSkipBackupAttributeToItemAtURL:path];
if (success)
NSLog(@"Marked %@", path);
else
NSLog(@"Can't marked %@", path);
}
}
path = [path stringByAppendingPathComponent:fileName];
return path;
}
/*
set the document files attribute to marked "do not backup"
*/
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSString *)path
{
const char* filePath = [path fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
iOS5是BOOL变量:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
iOS5 = NO;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0.1"))
iOS5 = YES;
感谢。
答案 0 :(得分:2)
#include <sys/xattr.h>
- (void) AddSkipBackupAttributeToFile: (NSURL*) url
{
u_int8_t b = 1;
setxattr([[url path] fileSystemRepresentation], "com.apple.MobileBackup", &b, 1, 0, 0);
}
答案 1 :(得分:1)
您无法在运行时写入Bundle。您只能在创建二进制文件时向捆绑包添加资源。您需要将解压缩过程的结果放入缓存目录。
由于系统可以随时删除此文件夹的内容,因此您应该在每次启动时检查是否需要再次解压缩该文件。
您可能只需要将上述方法的第一行更改为:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);