iPhone - 在本地和生产环境设置之间切换
我正在开发一个 iPhone 应用程序,它使用某处的服务器来获取其数据。在应用程序源代码的某个位置,我对用于连接的 URL 进行了硬编码。这很好,只是我并不总是想使用生产服务器进行测试!我不想弄乱实时数据,只是为了在本地测试一些东西。所以我设置了同一服务器的本地版本。但为了让 iPhone 应用程序使用该服务器,需要更改源代码中的硬编码 URL。
如果您经常在两台服务器之间切换,那么这样做有点麻烦。另外,我可能会不小心发布仍然使用本地 URL 的应用程序!
我想也许 XCode 可以帮助我解决这个问题,因为它有“调试”和“发布”配置选项的概念来构建。所以我的问题是:我能否以某种方式更改调试配置,使其指向本地服务器 URL?也许通过指向包含环境特定 URL 的属性或 plist 文件。然后,我可以创建此属性文件的两个版本,并使调试配置指向一个版本,同时使发布配置指向另一个版本。
有谁知道我怎样才能做到这一点?
I am developing an iPhone app which uses a server somewhere to fetch its data. Somewhere in the app's source code I hardcoded the URL to use to connect to. This is fine, except that I don't always want to test using a production server! I don't want to mess with live data, just to test something locally. So I set up a local version of that same server. But in order to make the iPhone app use that server is to change the hardcoded URL in the source code.
This is a little bit of a pain in the ass to do if you're often switching between the two servers. Also, I might accidentally release the app which still uses the local URL!
I was thinking that maybe XCode can help me with this since it has the notion of a "Debug" and a "Release" configuration option to build with. So my question is: can I somehow change the Debug configuration in a way that it points to local server URL? Maybe through pointing to a properties or plist file which contains the environment specific URL. I could then make two versions of this properties file and make the debug configuration point to one, while make the release configuration point to the other.
Does anyone know how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将此代码放在需要使用基于模式(调试/发布)=(开发/生产)的配置的位置。
放置它的最佳位置是“ProjectName”_Prefix.pch 文件。
干杯,
虚拟网络
Put this code where you need to use the configuration based on the mode (debug/release) = (development/production).
The best place to put it is on the "ProjectName"_Prefix.pch file.
Cheers,
VFN
在您的头文件之一(例如预编译头文件)中,使用 URL 定义宏。看看这篇文章使用类似的方法。
顺便说一句,我在我的所有应用程序中使用本文中的日志记录方法 - 它就像一个魅力,我强烈推荐它!
In one of your header files (such as the pre-compiled header file) define macros with the URL. Take a look at this article and use a similar approach.
Incidentally, I'm using the logging approach from this article in all my apps - it works like a charm, I strongly recommend it!
您可以通过简单地编辑 gcc 语言设置来定义 xcode 中的预处理器宏:
转到“项目”菜单并选择“编辑项目设置”。转到“构建”选项卡。
转至标有“GCC 4.0 - 语言”的部分。有一个名为“Other C Flags”的设置。添加您想要的所有“-Dwhatever”宏。
You can define pre-proccessor macros in xcode by simply editing the gcc language settings:
Go to the Project menu and select "Edit Project Settings". Go to the "Build" tab .
Go to the section labeled "GCC 4.0 - Language". There is a setting named "Other C Flags". Add all the "-Dwhatever" macros you want there.