Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Build Settings in Xcode 6?

Tags:

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.

like image 737
netwire Avatar asked Jul 09 '14 03:07

netwire


People also ask

How do I open build settings in Xcode?

Choose the project in the Project Navigator on the left. Select the Configurations target from the Targets section and click the Build Settings tab at the top. The Build Settings tab shows the build settings for the Configurations target. It's possible to expand this list with build settings that you define.

Where is build in Xcode?

It should by located in: ~/Library/Developer/Xcode/DerivedData .

How do I build in Xcode?

Launch Xcode, then click “Create a new Xcode project” in the Welcome to Xcode window or choose File > New > Project. In the sheet that appears, select the target operating system or platform and a template under Application. In the following sheets, fill out the forms and choose options to configure your project.

What is build configuration in Xcode?

A build configuration specifies a set of build settings used to build a target's product in a particular way. For example, it is common to have separate build configurations for debug and release builds of a product. A build setting in Xcode has two parts: the setting title and the definition.

How do I change the target build in Xcode?

In Xcode, choose Help > Xcode Help, or open the Xcode Help website. 2. Search for “build settings.” These build settings specify properties of the product the target builds. Space-separated list of identifiers. Specifies the architectures (ABIs, processor models) to which the binary is targeted.

What is the build setting title in Xcode?

The build setting title identifies the build setting and can be used within other settings. The build setting definition is a constant or a formula Xcode uses to determine the value of the build setting at build time. A build setting may also have a display name, which is used to display the build setting in the Xcode user interface.

How do I get the value of a variant in Xcode?

For each build variant in BUILD_VARIANTS, Xcode generates an OBJECT_FILE_DIR build setting with the variant name as a suffix. The generated build setting’s value is computed using OBJECT_FILE_DIR and the build variant name.


1 Answers

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.

like image 61
Albert Bori Avatar answered Oct 06 '22 10:10

Albert Bori