osx - bundle identifier and preferences? -
i know if nsuserfedault tied bundle identifier ?
for example have version 1.0 bundle identifier com.something , app saved settings in nsuserdefault standarddefaults
now sometime later if update comes , required change bundle identifier reason, new version able access settings/preferences stored previous version ?
the app not published app store has package installer
if change bundle identifier, you’ll have different set of user defaults. can still read old defaults provided application not sandboxed. example, if old bundle identifier com.company.aaa, can use following code copy old defaults new application+bundle identifier when application starts:
// read old defaults com.company.aaa nsuserdefaults *olddefaults = [nsuserdefaults new]; nsdictionary *olddefaultsdict = [olddefaults persistentdomainforname:@"com.company.aaa"]; // store old defaults in standard user defaults [[nsuserdefaults standarduserdefaults] setpersistentdomain:olddefaultsdict forname:[[nsbundle mainbundle] bundleidentifier]]];
you’ll want store defaults flag indicate you’ve imported old defaults once in order avoid rewriting them subsequently. example:
nsuserdefaults *currentdefaults = [nsuserdefaults standarduserdefaults]; if (! [currentdefaults boolforkey:@"importedolddefaultsfromaaa"]) { // read old defaults com.company.aaa nsuserdefaults *olddefaults = [nsuserdefaults new]; nsdictionary *olddefaultsdict = [olddefaults persistentdomainforname:@"com.company.aaa"]; // store old defaults in standard user defaults [currentdefaults setpersistentdomain:olddefaultsdict forname:[[nsbundle mainbundle] bundleidentifier]]]; // set flag avoid subsequent import of old defaults [currentdefaults setbool:yes forkey:@"importedolddefaultsfromaaa"]; }
Comments
Post a Comment