Reputation: 2847
what could you recommend to store settings (API endpoint, different keys - for Facebook, twitter etc) for iOS application written on Swift? I need something to easily switch between environments - development (local iOS simulator), staging (staging server) and production (for AppStore). One of solutions i saw is the creation of class Settings with predefined constants with app settings. Is it good enough? Or maybe you can recommend me something "swift way"?
PS: if you're familiar with ruby on rails - i'm looking something like figaro gem (https://github.com/laserlemon/figaro)
Upvotes: 1
Views: 538
Reputation: 2006
Add per-target/-configuration preprocessor defines in the Build Settings (the key you're looking for is Preprocessor Macros (under Apple LLVM - Processing)). You can set a compile time definition RELEASE
by appending RELEASE=1 to the preprocess macro field.
I've used this method for building lite and regular versions of the same app. I have one target with two configurations. Then I have #ifdef statements in my code to trigger specific features, etc.
The benefit of this is that you can customize for each target or configuration just the way you want depending on how you've laid out your project structure (target vs configuration).
Upvotes: 0
Reputation: 29886
Create a plist with the keys for each environment or load the directly
Create a target for each environment and add build flags/correct plist as needed in each.
Then add macros (still available in swift)
#if DEBUG
// set the debug keys here or load the plist
#else if RELEASE
// set the release keys here or load the plist
#else if STAGING
// load the staging keys or load the plist
#endif
Use the keys as needed
A better solution could be to fetch the keys from a server posting the environment flag
Upvotes: 2