如何在Xcode 6中访问Build Settings? [英] How to access Build Settings in Xcode 6?
问题描述
如何访问Build Settings变量似乎有很多变化(即为不同的 Debug
与<$定义Web服务的基本URL c $ c>发布环境)。
There seems to be a lot of variations of how to access the Build Settings variables (i.e. to define the base URL of a web service for different Debug
vs. Release
environments).
我在Project - > Building Settings中创建了一个User-Defined变量,每个环境一个。我们称之为 WEB_SERVICE_BASE_URL
。
I created a User-Defined variable in Project -> Building Settings, one for each environment. Let's call it WEB_SERVICE_BASE_URL
.
如何在代码中访问它?我正在使用XCode 6和Swift。
How do I access it in the code? I'm using XCode 6 and Swift.
我试过这个但它不起作用
I've tried this but it doesn't work
let api_key = ${WEB_SERVICE_BASE_URL}
我也尝试过这也不起作用
I've also tried this and it also doesn't work
let api_key = NSUserDefaults.standardUserDefaults().stringForKey("WEB_SERVICE_BASE_URL")
有什么建议吗?这似乎是一个经常需要的解决方案,它在Rails中非常容易,但在iOS开发中并非如此。
Any suggestions? This seems to be a often needed solution, it's so easy in Rails, but not so in iOS development.
推荐答案
以下是如何设置:
- 将用户定义的设置添加到目标的
构建设置
(您使用WEB_SERVICE_BASE_URL
) - 向目标的
Info.plist $添加新行c $ c>带密钥的文件:
WEB_SERVICE_BASE_URL
,输入:字符串
,值:$ { WEB_SERVICE_BASE_URL}
- Add a User-Defined setting to your target's
Build Settings
(which you did withWEB_SERVICE_BASE_URL
) - Add a new row to your target's
Info.plist
file with key:WEB_SERVICE_BASE_URL
, type:String
, value:${WEB_SERVICE_BASE_URL}
以下是获取价值的方法:
Here's how get the value:
let api_key = Bundle.main.object(forInfoDictionaryKey: "WEB_SERVICE_BASE_URL") as? String
注意:这些键/值可以从包中提取,所以一定要避免将敏感数据存储在那里。
这篇关于如何在Xcode 6中访问Build Settings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!