Home » Swift » How to access Build Settings in Xcode 6?

How to access Build Settings in Xcode 6?

Posted by: admin November 30, 2017 Leave a comment


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).

I created a User-Defined variable in Project -> Building Settings, one for each environment. Let’s call it WEB_SERVICE_BASE_URL.

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")

Any suggestions? This seems to be a often needed solution, it’s so easy in Rails, but not so in iOS development.


Here’s how to set it up:

  1. Add a User-Defined setting to your target’s Build Settings (which you did with WEB_SERVICE_BASE_URL)
  2. 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

Note: These keys/values can be extracted from the package, so be sure to avoid storing sensitive data in there.


The easiest approach is to manually modify the configuration each time
you switch environments. This means modifying a compiler flag or
manually modifying values in your project. This approach is error
prone, tedious, and far from ideal. A better solution is to create a
custom configuration for each environment. This involves creating a
configuration file that centralizes environment variables and custom
Xcode schemes.

Follow this tutorial, though it uses objective c, it’s pretty easy converting to swift.