在构建时覆盖 NSUserDefault 首选项
Override NSUserDefault preferences at build time
我希望能够在构建时在我的 iOS 项目中设置应用首选项。我知道我可以在 xcode 中创建不同的目标,但我认为随着我可能最终做出的偏好数量的增加,我最终可能会在我的项目中出现噩梦般的目标数量。
一个简单的示例是为名为 'amount' 的默认值设置默认整数。目前 'amount' 在我的应用程序中名为 'preferences.plist' 的 plist 文件中定义。我加载该 plist 文件并在 NSUserDefaults 上使用该 plist 注册默认值。
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
我想我可以写一个脚本来 mod 在构建之前验证 preferences.plist 文件,然后再构建它。但是,我认为当我需要 mod 一堆不同的偏好时可能会成为一场噩梦。
结束游戏是让 jenkins 构建我的 IPA。我想轻松创建多个 jenkins 构建,这些构建将指向相同的代码,但使用不同的首选项构建我的应用程序。
Android 具有风格和设置资源值的能力。 iOS 是否有类似的东西可以用来构建这些不同的 'flavors' 应用程序?
我没有足够的 Android 工作经验。
我会用多个 plist 来解决这个问题.. 每种口味一个..
我会尝试以下任一选项 --
我会让 Jenkins 根据我要构建的风格交换 plist。脚本将为给定的风格选择正确的 plist
我将为每种风格定义编译时宏并加载适当的 plist.. 像这样
#ifdef FLAVOUR1
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour1" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
#endif
#ifdef FLAVOUR2
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour2" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
#endif
#ifdef FLAVOUR2
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour3" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
#endif
我使用 Jenkins 构建操作在 Xcode 构建之前将适当的变量注入到 plist 中:
plutil -replace MyBuildBranch -string ${BRANCH} MyProj/MyProj-Info.plist
然后我在运行时使用类似以下内容读取该值:
NSBundle * bundle = [NSBundle bundleForClass:[AppDelegate class]];
NSString * myBuildBranch = bundle.infoDictionary[@"MyBuildBranch"]
我希望能够在构建时在我的 iOS 项目中设置应用首选项。我知道我可以在 xcode 中创建不同的目标,但我认为随着我可能最终做出的偏好数量的增加,我最终可能会在我的项目中出现噩梦般的目标数量。
一个简单的示例是为名为 'amount' 的默认值设置默认整数。目前 'amount' 在我的应用程序中名为 'preferences.plist' 的 plist 文件中定义。我加载该 plist 文件并在 NSUserDefaults 上使用该 plist 注册默认值。
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
我想我可以写一个脚本来 mod 在构建之前验证 preferences.plist 文件,然后再构建它。但是,我认为当我需要 mod 一堆不同的偏好时可能会成为一场噩梦。
结束游戏是让 jenkins 构建我的 IPA。我想轻松创建多个 jenkins 构建,这些构建将指向相同的代码,但使用不同的首选项构建我的应用程序。
Android 具有风格和设置资源值的能力。 iOS 是否有类似的东西可以用来构建这些不同的 'flavors' 应用程序?
我没有足够的 Android 工作经验。
我会用多个 plist 来解决这个问题.. 每种口味一个..
我会尝试以下任一选项 --
我会让 Jenkins 根据我要构建的风格交换 plist。脚本将为给定的风格选择正确的 plist
我将为每种风格定义编译时宏并加载适当的 plist.. 像这样
#ifdef FLAVOUR1 NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour1" withExtension:@"plist"]; NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile]; [[NSUserDefaults standardUserDefaults] registerDefaults:preferences]; #endif #ifdef FLAVOUR2 NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour2" withExtension:@"plist"]; NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile]; [[NSUserDefaults standardUserDefaults] registerDefaults:preferences]; #endif #ifdef FLAVOUR2 NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences-flavour3" withExtension:@"plist"]; NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile]; [[NSUserDefaults standardUserDefaults] registerDefaults:preferences]; #endif
我使用 Jenkins 构建操作在 Xcode 构建之前将适当的变量注入到 plist 中:
plutil -replace MyBuildBranch -string ${BRANCH} MyProj/MyProj-Info.plist
然后我在运行时使用类似以下内容读取该值:
NSBundle * bundle = [NSBundle bundleForClass:[AppDelegate class]];
NSString * myBuildBranch = bundle.infoDictionary[@"MyBuildBranch"]