Simple title output in ios settings bundle

后端 未结 6 1968
名媛妹妹
名媛妹妹 2021-02-01 21:17

I just want to output the version number of my ios application in a settings file.

I understand that I have to add the settings file to the application folder.

W

6条回答
  •  悲哀的现实
    2021-02-01 21:38

    You can sync the Settings bundle to NSUserDefaults, but weirdly it does not do at first. You have to first retrieve the values from Settings to NSUserDefaults and then after that, edit that you make to those values in NSUserDefaults are automatically applied to Settings bundle.

    I referenced this nice article.

    EDIT:

    For your case to just to save your version, something like this would work. (This sample code is overkill in a way, but should be simpler to understand the flow)

    //Get the bundle file
    NSString *bPath = [[NSBundle mainBundle] bundlePath];
    NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
    NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
    
    //Get the Preferences Array from the dictionary
    NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
    NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];
    
    //Save default value of "version_number" in preference to NSUserDefaults 
    for(NSDictionary * item in preferencesArray) {
        if([[item objectForKey:@"key"] isEqualToString:@"version_number"]) {
            NSString * defaultValue = [item objectForKey:@"DefaultValue"];
            [[NSUserDefaults standardUserDefaults] setObject:defaultValue forKey:@"version_number"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
    
    //Save your real version number to NSUserDefaults
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    [[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
    

提交回复
热议问题