ios 开发项目初始化配置_如何为iOS项目管理不同的环境和配置

ios 开发项目初始化配置

As iOS developers, we are already aware of managing different environments like Development, QA, Beta, and Production. For these different environments, there are different server URLs, app icons, and configurations.

作为iOS开发人员,我们已经意识到要管理不同的环境,例如开发,质量检查,测试版和生产。 对于这些不同的环境,有不同的服务器URL,应用程序图标和配置。

So before creating a new build pointing to an environment, we need to keep in mind that we also have to change the server URL. We could do this by changing some hardcoded flag value in the constant file or using macros, but it makes everything more complicated.

因此,在创建指向环境的新版本之前,我们需要记住,我们还必须更改服务器URL。 我们可以通过更改常量文件中的某些硬编码标志值或使用宏来完成此操作,但这会使一切变得更加复杂。

But if we think for a little while, we can come up with an idea. And by applying this idea, we can easily handle any scenario. So the idea is, if we create different schemas and configurations, then it allows us to change the application server URLs, App icon, Plist file, and configuration.

但是,如果我们想一会儿,我们可以提出一个想法。 通过应用这种想法,我们可以轻松应对任何情况。 因此,想法是,如果我们创建不同的架构和配置,那么它允许我们更改应用程序服务器URL,应用程序图标,Plist文件和配置。

In this tutorial, I will show you how to manage different environments using schemas and configuration.

在本教程中,我将向您展示如何使用架构和配置来管理不同的环境。

These are the steps:

这些步骤是:

项目设置: (Project Setup:)

Open XCode and create a new single view application with a proper name.

打开XCode并使用适当的名称创建一个新的单视图应用程序。

添加架构和配置: (Add Schema and Configurations:)

Before adding a schema, we need to know that every XCode schema comes with two different build configurations: Debug and Release. Then if we want, we can make changes specific to a particular build configuration.

在添加架构之前,我们需要知道每个XCode架构都带有两种不同的构建配置: DebugRelease。 然后,如果需要,我们可以针对特定的构建配置进行更改。

Now to add our build configurations, select the project in the Project Navigator pane on the left. Then select Info from the two options (Info and Build Settings). In the Configurations, we have to add our own configuration for the five environments (Development, Production, QA, Beta, and UAT) there.

现在要添加我们的构建配置,请在左侧的“ 项目导航器”窗格中选择项目。 然后,从两个选项信息 ( 信息构建设置)。Configurations中 ,我们必须为其中的五个环境(开发,生产,QA,Beta和UAT)添加我们自己的配置。

First of all, double click on Debug and rename it as Debug (Development). Similarly, double click on Release and rename it as Release (Development). Now click +, and select Duplicate Debug (Development) and Duplicate Release (Development), then change the duplicate environment name with the others available names.

首先,双击“ 调试”并将其重命名为“ 调试(开发)”。 同样,双击发布并将其重命名为发布(开发)。 现在单击+,然后选择Duplicate Debug(开发)Duplicate Release(Development),然后使用其他可用名称更改重复的环境名称。

For the schema creation, go to manage schema in top left corner of XCode. There you can see that one schema is already available. Rename it as Development — or you can delete the existing and add a new one with the name Development. Then add the rest of the four schemas for the other environments.

对于架构创建 ,请转到XCode左上角的管理架构。 在那里您可以看到一个模式已经可用。 将其重命名为Development,或者您可以删除现有名称并添加一个名称为Development的新名称。 然后为其他环境添加其余四个模式。

Oops, don’t forget to check the shared box there. After adding all the schemas, the manage schema screen should look like this:

糟糕,不要忘记在此处选中共享框。 添加所有架构后,管理架构屏幕应如下所示:

添加配置设置文件: (Add Configuration Settings File:)

Right click on Project, select new file, then add Configuration Settings File and give it the same name as the environment.

右键单击Project,选择新文件,然后添加Configuration Settings File并为其指定与环境相同的名称。

After adding all the config files, your Project Navigator left pane should look like this:

添加所有配置文件后,Project Navigator的左窗格应如下所示:

Now the most important part starts: add your server URL and other customized key value in the corresponding configuration file.

现在最重要的部分开始:在相应的配置文件中添加服务器URL和其他自定义键值。

添加Plist文件: (Add Plist Files:)

Rename the info.plist file as development.plist. Copy and paste the same plist file for the different environments inside the project, and rename each of the plist file with the environment’s name. You can set some environment-specific keys and values in the plist files. After that, add the keys from the configuration file to the plist files like this:

将info.plist文件重命名为development.plist。 为项目内的不同环境复制并粘贴相同的plist文件,然后使用环境名称重命名每个plist文件。 您可以在plist文件中设置一些特定于环境的键和值。 之后,将密钥从配置文件添加到plist文件,如下所示:

Now we have to set the appropriate plist path for each build configuration. From Targets, just select a plist file, and rename it with the same name for the Debug and Release configuration.

现在,我们必须为每个构建配置设置适当的plist路径。 在Tar​​gets中,只需选择一个plist文件,然后使用与Debug and Release配置相同的名称对其进行重命名。

将构建配置链接到配置文件: (Linking Build Configuration with the Configuration File:)

Select all the build configurations (Debug and Release) in Projects Info one by one. Then set the appropriate configuration file, which you have added to the project.

项目信息中一一选择所有构建配置(调试和发布)。 然后设置适当的配置文件,该文件已添加到项目中。

After adding all the configuration files, your Build settings should look like this:

添加所有配置文件后,您的构建设置应如下所示:

So we have now successfully linked all the configuration files to the respective build configurations.

因此,我们现在已经成功地将所有配置文件链接到各自的构建配置。

将架构与构建配置链接: (Linking Schema with the Build Configuration:)

Now the last step is linking the schema with the build configuration. To do this, select any schema, go to edit schema, and set the appropriate build configuration there.

现在,最后一步是将架构与构建配置链接。 为此,选择任何模式,转到编辑模式,然后在此处设置适当的构建配置。

准备运行项目: (Ready to Run the Project:)

Now all the set up is done. The only thing you have to do select the schema and run — the environment will be automatically selected for you. So for fetching the server URL and other values, I have created an Environment.swift file. Check it out:

现在,所有设置均已完成。 您唯一要做的就是选择架构并运行-环境将自动为您选择。 因此,为了获取服务器URL和其他值,我创建了Environment.swift文件。 看看这个:

To fetch the server URL or other settings in ViewController.Swift or any other file, you have to write only one line of code:

要在ViewController.Swift或任何其他文件中获取服务器URL或其他设置,您只需要编写一行代码:

You can also manage different app icons for different environments from build settings. Then, you will only have to look for a second to see which environment build is installed on your device.

您还可以通过构建设置为不同的环境管理不同的应用程序图标。 然后,您只需等待一秒钟即可查看设备上安装了哪个环境版本。

The complete project is available on GitHub. You can download it if you have any questions.

完整的项目可在GitHub找到 。 如有任何疑问,可以下载。

Don’t spend extra time changing the server URL or some other configuration every time you build your project. This is the easiest way to manage different environments, app icons, and configurations.

不要在每次构建项目时都花费额外的时间来更改服务器URL或其他配置。 这是管理不同环境,应用程序图标和配置的最简单方法。

If you like this, please don’t forget to give a clap. It will inspire me more. For any suggestions, please feel free to write an email to mail2boudhayan@gmail.com.

如果您喜欢这个,请别忘了鼓掌。 它会启发我更多。 如有任何建议,请随时发送电子邮件至mail2boudhayan@gmail.com。

翻译自: https://www.freecodecamp.org/news/managing-different-environments-and-configurations-for-ios-projects-7970327dd9c9/

ios 开发项目初始化配置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值