iPhone:将资源移至应用程序包
我的申请刚刚被 Apple 拒绝,因为我将资源存储在 Documents 文件夹下! Documents 文件夹会自动同步到 iCloude,因此只有用户生成的数据才应存储在 Documents 下。所有应用程序数据都应位于应用程序包下。
我在整个项目中使用以下方法
- (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;
谢谢。
My application just got rejected from Apple because I store my resources under Documents folder! The Documents folder gets synched to iCloude automatically so only user generated data should be stored under Documents. All application data should go under Application bundle.
I use following method through out my project
- (NSString *)filePath:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
return path;
}
For example, following line unzip my resources under documents folder.
[SSZipArchive unzipFileAtPath: zipFileName toDestination:[self filePath: @""]];
How can I move these files and Resources folder images into Application bundle and access them?
[EDIT]
- (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;
}
Where iOS5 is BOOL variable:
#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;
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 iOS 5.0.1 中,您可以添加新的文件属性以将它们标记为非 iCloud 同步:
in iOS 5.0.1 yo can add a new file attributo to mark them as non iCloud sync:
您无法在运行时写入 Bundle。您只能在创建二进制文件时将资源添加到捆绑包中。您需要将解压缩过程的结果放入缓存目录中。
由于该文件夹的内容可以随时被系统删除,因此您应该在每次启动时检查该文件是否需要再次解压。
您可能只需要将上述方法的第一行更改为:
You cannot write to the Bundle at runtime. You can only add resources to the bundle when creating the binary. You will need to place the results of the unzip process into the Caches Directory.
Because the contents of this folder can be deleted at any time by the system, you should check at every startup if the file needs to be unzipped again.
You will likely only need to change the first line of the above method to: