Ios 在生成时覆盖NSUserDefault首选项
我希望能够在iOS项目的构建时设置应用程序首选项。我知道我可以在xcode中创建不同的目标,但我认为,随着我的首选项数量的增加,我的项目中可能会有大量的目标 一个简单的例子是为名为“amount”的默认值设置默认整数。当前“金额”是在我的应用程序中名为“preferences.plist”的plist文件中定义的。我加载该plist文件并在NSUserDefaults上使用该plist注册默认值Ios 在生成时覆盖NSUserDefault首选项,ios,objective-c,xcode,plist,nsuserdefaults,Ios,Objective C,Xcode,Plist,Nsuserdefaults,我希望能够在iOS项目的构建时设置应用程序首选项。我知道我可以在xcode中创建不同的目标,但我认为,随着我的首选项数量的增加,我的项目中可能会有大量的目标 一个简单的例子是为名为“amount”的默认值设置默认整数。当前“金额”是在我的应用程序中名为“preferences.plist”的plist文件中定义的。我加载该plist文件并在NSUserDefaults上使用该plist注册默认值 NSURL *preferencesFile = [[NSBundle mainBundle] UR
NSURL *preferencesFile = [[NSBundle mainBundle] URLForResource:@"preferences" withExtension:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfURL:defaultPreferencesFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:preferences];
我假设我可以在构建preferences.plist文件之前编写一个脚本来修改它,然后再构建它。然而,我认为当我需要修改一系列不同的偏好时,可能会成为一场噩梦
游戏的最后一步是让詹金斯建造我的IPA。我想轻松创建多个jenkins版本,这些版本将指向相同的基于代码的应用程序,但使用不同的首选项构建我的应用程序
Android有自己的风格,并且能够设置资源值。iOS是否有类似的功能,我可以用来构建这些不同“风格”的应用程序?我没有足够的Android工作经验 我会用多种方法来解决这个问题。。每种口味一个 我将尝试下面的任一选项--
#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
在Xcode构建之前,我使用Jenkins构建操作将适当的变量注入plist:
plutil -replace MyBuildBranch -string ${BRANCH} MyProj/MyProj-Info.plist
然后,我在运行时使用以下方法读取该值:
NSBundle * bundle = [NSBundle bundleForClass:[AppDelegate class]];
NSString * myBuildBranch = bundle.infoDictionary[@"MyBuildBranch"]
很好,我不知道plutil的事。听起来很完美。谢谢