【问题标题】:How to Change iphone Application Preferences Settings at run time?如何在运行时更改 iphone 应用程序首选项设置?
【发布时间】:2011-01-22 12:13:14
【问题描述】:

目标:我想在运行时设置 iphone Application Preferences

在 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];

【问题讨论】:

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


    【解决方案1】:

    你的包是只读的,所以你想写的任何东西都需要外部化。如果您愿意,可以使用标准默认值来保留所有这些信息。这很简单:

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

    【讨论】:

      【解决方案2】:

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

      捆绑包是只读的。您不能在运行时更新捆绑包。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多