Xcode 使用方案来确定开发/登台/生产服务器 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20863628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:29:28  来源:igfitidea点击:

Xcode using schemes to determine dev/staging/production server URLs

iosobjective-cxcode

提问by bennythemink

I wish to use Xcode's schemes to determine what server to run my app against. I have an app that fetches its information from a server. Like most people I have a development server, a staging server and a production server.

我希望使用 Xcode 的方案来确定运行我的应用程序的服务器。我有一个从服务器获取信息的应用程序。像大多数人一样,我有一个开发服务器、一个临时服务器和一个生产服务器。

I wish to create a scheme for each of these where Xcode will inject the correct URL. Do I duplicate the run scheme and add environmental variables? Is this the best way to do things, I don't particularly wish to have #ifdef's in my server class and setting it in code each time I change server. Can anyone point me in the right direction please?

我希望为其中的每一个创建一个方案,其中 Xcode 将注入正确的 URL。我是否复制运行方案并添加环境变量?这是最好的做事方式吗,我不特别希望在我的服务器类中有 #ifdef 并在每次更改服务器时在代码中设置它。任何人都可以指出我正确的方向吗?

FYI: I'm using Xcode 5.0.2 iOS7 SDK.

仅供参考:我使用的是 Xcode 5.0.2 iOS7 SDK。

[EDIT] Everyone gave some great suggestions but I feel @redent84 answer best suits my needs. Though I found it interesting that none actually suggested using different schemes. [/EDIT]

[编辑] 每个人都提出了一些很好的建议,但我觉得@redent84 的答案最适合我的需求。尽管我发现有趣的是实际上没有人建议使用不同的方案。[/编辑]

采纳答案by redent84

I recommend you to create different XCode Targetsfor each environment. I recommend you to change the App Identifier of the Apps, for example, the production app would be com.mycompany.Appand the DEVEL version would be com.mycompany.App-DEVEL. This way you can track the Apps separately in HockeyApp or TestFlight, and you can have both applications in the same device at the same time.

我建议您为每个环境创建不同的XCode 目标。我建议您更改应用程序的应用程序标识符,例如,生产应用程序为com.mycompany.App,开发版本为com.mycompany.App-DEVEL。这样您就可以在 HockeyApp 或 TestFlight 中分别跟踪应用程序,并且您可以同时在同一设备中拥有这两个应用程序。

Then, add Preprocessor Macros that define the environment for every target. DEVELfor development, for example.

然后,添加为每个目标定义环境的预处理器宏。DEVEL例如,用于开发。

If the URL is hardcoded, simply add a #ifdefinstruction to choose the URL:

如果 URL 是硬编码的,只需添加一条#ifdef指令来选择 URL:

#ifdef DEVEL
#define ENDPOINT_URL @"http://develserver:8080/EndPoint"
#elif defined(STAGING)
#define ENDPOINT_URL @"http://stagingserver:8080/EndPoint"
#else
#define ENDPOINT_URL @"http://app.mycompany.com/EndPoint"
#endif

This way is less error-prone to distribute a development version, easier to maintain and allows you to add custom code to different versions. For example, you may want to include the version number in the login screen or show alert dialogs for development, but not for distribution version.

这种方式在分发开发版本时不易出错,更易于维护,并允许您将自定义代码添加到不同版本。例如,您可能希望在登录屏幕中包含版本号或显示用于开发的警报对话框,而不是用于分发版本。

回答by rufo

Use a key in the plist (for each schema, eg: URL_TO_USE), use define to create a 'shortcut' to get the value.

在 plist 中使用一个键(对于每个模式,例如:URL_TO_USE),使用定义创建一个“快捷方式”来获取值。

  #define MyURL [[NSBundle mainBundle] objectForInfoDictionaryKey:@"URL_TO_USE"]

EDIT

编辑

You must have multiple targets. Each target should point to a different .plist file. See: How to configure independent sets of runtime settings in XCode

您必须有多个目标。每个目标都应该指向不同的 .plist 文件。请参阅: 如何在 XCode 中配置独立的运行时设置集

回答by Matt Jennings

I handle this by setting custom pre-processor defines for each scheme.

我通过为每个方案设置自定义预处理器来处理这个问题。