[英]How to access Build Settings in Xcode 6?
There seems to be a lot of variations of how to access the Build Settings variables (ie to define the base URL of a web service for different Debug
vs. Release
environments). 似乎有很多关于如何访问Build Settings变量的变体(即为不同的
Debug
vs. Release
环境定义Web服务的基本URL)。
I created a User-Defined variable in Project -> Building Settings, one for each environment. 我在Project - > Building Settings中创建了一个User-Defined变量,每个环境都有一个变量。 Let's call it
WEB_SERVICE_BASE_URL
. 我们称之为
WEB_SERVICE_BASE_URL
。
How do I access it in the code? 如何在代码中访问它? I'm using XCode 6 and Swift.
我正在使用XCode 6和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")
Any suggestions? 有什么建议么? This seems to be a often needed solution, it's so easy in Rails, but not so in iOS development.
这似乎是一个经常需要的解决方案,它在Rails中非常容易,但在iOS开发中并非如此。
Here's how to set it up: 以下是如何设置它:
Build Settings
(which you did with WEB_SERVICE_BASE_URL
) Build Settings
(使用WEB_SERVICE_BASE_URL
) Info.plist
file with key: WEB_SERVICE_BASE_URL
, type: String
, value: ${WEB_SERVICE_BASE_URL}
Info.plist
文件添加新行: WEB_SERVICE_BASE_URL
,类型: String
,值: ${WEB_SERVICE_BASE_URL}
Here's how get the value: 以下是获取价值的方法:
let api_key = Bundle.main.object(forInfoDictionaryKey: "WEB_SERVICE_BASE_URL") as? String
Note: These keys/values can be extracted from the package, so be sure to avoid storing sensitive data in there. 注意:可以从包中提取这些键/值,因此请务必避免在其中存储敏感数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.