Zorayr
Zorayr

Reputation: 24922

iOS/Swift/Xcode: Recommended place to put application wide keys, properties and configs?

I have application wide config variables such as mixpanel token, twillio access token, etc. that currently I am storing as global constants in my AppDelegate. Is there a better to put thesee?


// TODO: Is there a better place to put these?
let appId = 1.....2
let contactUsPhoneNumber = +17.......2
let mixpanelToken = "1..............2"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
}

Some options I have considered:

  1. Global Variables — This seems like actually the best approach; super easy, quick, etc.
  2. "Configs" Class — Slightly more organized than #1
  3. Info.plist — Using XML, reading/writing it in Swift is a nightmare.
  4. Another.plist — Same as #3, but at least the configs are now stored separately from Apple's
  5. Config.json — Will have less of the XML nightmares, but still will need to read/write a file, which is annoying.

Anything else I am missing?

Upvotes: 0

Views: 294

Answers (1)

matt
matt

Reputation: 535304

Ideally keep these values in some class or struct that makes sense to own them. If there really is no such thing, then at least namespace them by declaring them as static let instance properties of an enum or struct. Do not let them "float free" as in your example code.

Upvotes: 1

Related Questions