Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide API keys in GitHub for iOS (SWIFT) projects?

Hello I'm trying to publish a iOS (SWIFT) personal project in GitHub but I'm afraid of sharing my private API keys and secrets with everybody.

I'm using parse so I have in my AppDelegate something like this:

let applicationId = "mySecretApplicationId" let clientKey = "mySecretClientKey" Parse.setApplicationId(applicationId!, clientKey: clientKey!) 

I would like to hide "mySecretApplicationId" and "mySecretClientKey", is there private place or directory in my project where I can put this variables?

Thanks!

like image 780
Manuel Martín Avatar asked Jun 12 '15 12:06

Manuel Martín


People also ask

How do I keep API key hidden on github?

The only way to hide it is to proxy your request through your own server. Netlify Functions are a free way to add some simple backend code to a frontend app. This is this method I used while learning to program in college, where I needed to share my progress with my peer group without disclosing my API keys.

How should you hide your API keys?

has suggested in a comment: if at some point you committed your API keys to your git repo, you should remove all traces of it. You can do this by using git rebase and removing the commit that added the keys.


1 Answers

You can use a .plist file where you store all your important keys. It is very important to put this file into your .gitignore file.

In your case, you need to set your keys.plist file like this: enter image description here

And use it inside your AppDelegate as follows:

    var keys: NSDictionary?      if let path = NSBundle.mainBundle().pathForResource("Keys", ofType: "plist") {         keys = NSDictionary(contentsOfFile: path)     }     if let dict = keys {         let applicationId = dict["parseApplicationId"] as? String         let clientKey = dict["parseClientKey"] as? String          // Initialize Parse.         Parse.setApplicationId(applicationId!, clientKey: clientKey!)     } 

SWIFT 3 Update:

 if let path = Bundle.main.path(forResource: "Keys", ofType: "plist") {         keys = NSDictionary(contentsOfFile: path)     } 
like image 94
EnriMR Avatar answered Sep 20 '22 00:09

EnriMR