iPhone:将资源移至应用程序捆绑包 [英] iPhone: move resources to Application bundle

查看:75
本文介绍了iPhone:将资源移至应用程序捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序刚刚被Apple拒绝了,因为我将资源存储在Documents文件夹下! Documents文件夹会自动同步到iCloude,因此仅用户生成的数据应存储在Documents下.所有应用程序数据都应位于应用程序捆绑包下.

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

例如,以下行将我的资源解压缩到documents文件夹下.

For example, following line unzip my resources under documents folder.

[SSZipArchive unzipFileAtPath:zipFileName toDestination:[self filePath:@"]];

[SSZipArchive unzipFileAtPath: zipFileName toDestination:[self filePath: @""]];

如何将这些文件和Resources文件夹图像移动到Application bundle中并访问它们?

How can I move these files and Resources folder images into Application bundle and access them?

- (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变量的地方:

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;

谢谢.

推荐答案

您无法在运行时写入捆绑软件.创建二进制文件时,只能将资源添加到捆绑软件中.您需要将解压缩过程的结果放入缓存目录"中.

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:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

这篇关于iPhone:将资源移至应用程序捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆