简体   繁体   English

如何使用外部文件.plist或.xcconfig启用部分代码

[英]How to enable parts of code with external files .plist or .xcconfig

I have a project that I'm using the fastlane toolchain to build. 我有一个项目正在使用fastlane工具链进行构建。

I have multiple deployment targets (hockey app, test flight, app store) - and for each deployment I'm interested in disabling and enabling certain features. 我有多个部署目标(曲棍球应用,试飞,应用商店)-对于每个部署,我都想禁用和启用某些功能。

For example - when i'm doing an alpha build with an enterprise cert to hockeyapp i want to disable flurry analytics. 例如-当我使用hockeyapp的企业证书进行Alpha构建时,我想禁用flurry分析。

When i'm doing a beta build I want to change the app-id and/or change the flurry key 当我进行Beta版构建时,我想更改应用程序ID和/或更改快速键

I currently have everything working in fastlane with a variety of sed commands to search and replace files etc. 我目前可以使用各种sed命令在fastlane中进行所有工作,以搜索和替换文件等。

That being said, I'm sure there is a better way. 话虽这么说,我相信还有更好的方法。

I know in Obj-c i can #ifdef against certain build environment flags to enable / disable parts of code. 我知道在Obj-c中我可以针对某些构建环境标志#ifdef来启用/禁用部分代码。 What is the best way to go about this in swift. 快速解决此问题的最佳方法是什么。

Do I use some sort of custom .plist file or .xcconfig setting to tell the app what to do/use or is there a different way? 我是否使用某种自定义.plist文件或.xcconfig设置来告诉应用程序要做什么/使用什么,或者有其他方法吗?

The things i'm interested in are 我感兴趣的是

  • Disabling portions of code ( ignore a certain function in a specific config) 禁用部分代码(忽略特定配置中的特定功能)
  • Changing values used in code aka API_KEY or something like that 更改代码(也称为API_KEY或类似名称)中使用的值

You can still use something similar to #ifdef preprocessor in swift, and you can combine it with .xcconfig which can be used to specify various variables. 您仍然可以迅速使用类似于#ifdef预处理程序的东西,并且可以将其与.xcconfig结合使用,后者可用于指定各种变量。 Your scripts can generate the values that will go into the .xcconfig vars. 您的脚本可以生成将进入.xcconfig变量的值。

The basic setup would be: 基本设置为:

  1. Create a .xcconfig file and associate it with your desired build configurations (in project settings -> info). 创建一个.xcconfig文件,并将其与所需的构建配置关联(在项目设置->信息中)。
  2. Add variables to the .xcconfig . 将变量添加到.xcconfig For example, if you want to activate ALPHA build you can do something like this: IS_ALPHA_BUILD=-D ALPHA . 例如,如果要激活ALPHA构建,可以执行以下操作: IS_ALPHA_BUILD=-D ALPHA Notice the -D which is necessary here. 注意此处需要的-D Your scripts can leave the variables empty if you want the flag turned off. 如果要关闭该标志,脚本可以将变量保留为空。
  3. In the build settings for your target, under "Swift Complier - Custom Flags" - > "Other Swift Flags" add the flag based on the .xcconfig file variables: ${IS_ALPHA_BUILD} . 在目标的构建设置中,在“快速编译器-自定义标志”->“其他快速标志”下,基于.xcconfig文件变量添加标志: ${IS_ALPHA_BUILD} If the .xcconfig is setup correctly you should see its content of the variable once you're done editing the variable. 如果正确设置了.xcconfig则在完成变量的编辑后,应该会看到其内容。
  4. Use the flags in your code: 在代码中使用标志:

     #if ALPHA print("alpha") #else print("not alpha") #endif 

I hope this helps, or that it will at least give you an idea for the best approach suitable for you. 我希望这会有所帮助,或者至少会为您提供适合您的最佳方法的想法。

I would suggest placing the optional code in separate files that implement it using extensions. 我建议将可选代码放在使用扩展名实现的单独文件中。 You can then enable/disable files using the file properties in the project Navigator. 然后,您可以使用项目导航器中的文件属性来启用/禁用文件。

I'm doing this even for a single application for which I have 3 targets in the project : full IOS version (paid), limited IOS version (free) and apple TV version. 即使对于一个项目中有3个目标的单个应用程序,我也要这样做:完整的IOS版本(付费),有限的IOS版本(免费)和Apple TV版本。

I also use the same technique to enable/disable stubs for whole classes that are used in several places but only actually work in one of the targets. 我还使用相同的技术来启用/禁用用于多个地方的整个类的存根,但实际上只能在其中一个目标中工作。

I have a similar conceptual problem. 我有一个类似的概念问题。 I want to use FDD and than use a mechanism to disable features as per need. 我想使用FDD,然后根据需要使用一种机制来禁用功能。 I am going to experiment with pList. 我将尝试使用pList。 My initial idea is to declare all features in plist and give them boolean values. 我最初的想法是在plist中声明所有功能,并为它们提供布尔值。 Thus, if a feature needs to be enabled, I will only enable it in plist. 因此,如果需要启用功能,我将仅在plist中启用它。

On the programming side, I am planning to write all features as extensions for parent data type with enclosing conditional statements. 在编程方面,我计划将所有功能都写为带有条件语句的父数据类型的扩展。 Therefore from perspective of controller , my code efforts will be reduced to just calling the behavior/ function. 因此,从控制器的角度来看,我的代码工作将减少为仅调用行为/函数。

I hope it made some sense, I will probably rewrite the answer sooner than later 我希望这是有道理的,我可能会迟早重写答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM