Reputation: 21
i'm coding an app with server API calls and I've got a private key for access, my question is: Where should I put that key? Let me explain a little more. When we work for example in JavaScript we have a .env file, it contains sensitive content and is in most of cases on .gitignore file, so where is a correct place to save and access to those private keys on Xcode? or is correct simply to do something like:
let key = "MY_KEY"
thanks in advance
Upvotes: 0
Views: 463
Reputation: 663
Even though what Codey said is correct but its much better to use a struct and create variables inside that struct. Simply making a variable 'key' might become troublesome in future as you might end up redeclaring it and losing everything. Also by using a struct, it gives much more clarity for the developer, it would make your code much more understandable.
Instead of going through that trouble do this.
Create a new swift file. Name is something like GlobalVariables.swift (or as Codey said). Now create a struct inside that file.
struct serverInfo
{
static let key = "KEY"
static let server = "0.0.0.0"
}
You can access these variables anywhere on the program with
let x = serverInfo.key
let y = serverInfo.server
Upvotes: 2
Reputation: 1233
If this is only for development purposes, then you can store your password in the code. But note, that this file shouldn't be uploaded to your repository.
I would suggest to create a separate file (like Config.swift
) and write your global configuration constants in this file.
// Config.swift
//
let key = "MY_KEY"
let server = "10.0.0.1"
You can then access these variables everywhere in your project. So this is the file you don't want to commit. Keep in mind, that everyone that uses your project needs such a file in order for the project to compile.
Upvotes: 2