如何在运行时更改iphone应用程序首选项设置?

时间:2011-01-22 12:13:14

标签: iphone objective-c nsuserdefaults preferences application-settings

目标:我想在运行时设置iphone应用程序首选项

在Setting.Bundle中我有title = Version和DefaultValue = 1.0

因此,在运行时如果有新版本可用,我必须更改iphone应用程序首选项默认值。但我无法这样做。它显示1.0而不是1.1

我不知道我的代码中有什么错误

代码:

 NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
if(!settingsBundle) {
    NSLog(@"Could not find Settings.bundle");
    return;
}

NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];

NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences) {
    NSString *key = [prefSpecification objectForKey:@"Key"];
    if(key) {

        if([key isEqualToString:@"name_preference"])
        {
            NSString *a=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
            [defaultsToRegister setObject:a forKey:key];
        }
        else {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];

        }


    }
}

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];

2 个答案:

答案 0 :(得分:2)

您的软件包是只读的,因此您要编写的任何内容都需要外部化。如果需要,您可以使用标准默认值来保留所有信息。这非常简单:

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
[standardDefaults setObject: build forKey:@"MyAppBuild"];
[standardDefaults setObject: version forKey:@"MyAppVersion"];
[standardDefaults synchronize]; 

此外,您可以将plist复制到设备文件系统并对其进行读写:

读:

+ (NSDictionary *) loadAppData {
    NSString *finalPath;

    // Load "defaults" plist
    NSArray *paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *writablePath = [documentsPath stringByAppendingPathComponent:@"AppSettings.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath: writablePath]){
        finalPath = writablePath;
    }
    else{
        NSString *path = [[NSBundle mainBundle] bundlePath];
        finalPath = [path stringByAppendingPathComponent:@"AppSettings.plist"];
    }

    return [NSDictionary dictionaryWithContentsOfFile: finalPath];
}

写作:

+ (void) saveAppData: (NSDictionary *) appData {

    // Load "defaults" plist
    NSArray *paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *writablePath = [documentsPath stringByAppendingPathComponent:@"AppSettings.plist"];

    // Save it
    [appData writeToFile: writablePath atomically: YES];
}

答案 1 :(得分:0)

我很难理解这个问题,而且你的代码也不容易理解(我从不使用registerDefaults和它所做的文档有点模糊)

捆绑包是只读的。您无法在运行时更新软件包。

“因此,在运行时如果有新版本可用,我必须更改iphone应用程序首选项默认值。” - 并不是的。版本值已经在捆绑包中(取自您的app.plist)

我使用以下代码行从捆绑包中读取应用版本值: NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];