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.
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.
It should by located in: ~/Library/Developer/Xcode/DerivedData .
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.
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.
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.
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.
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.
Here's how to set it up:
Build Settings
(which you did with WEB_SERVICE_BASE_URL
)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With