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

后端 未结 2 430
生来不讨喜
生来不讨喜 2021-01-31 09:29

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 ha

相关标签:
2条回答
  • 2021-01-31 10:16

    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)
        }
    
    0 讨论(0)
  • 2021-01-31 10:32

    Put them in a configuration file that you add to the .gitignore file. Check in a sample configuration file that every developer can use to create their own configuration.

    0 讨论(0)
提交回复
热议问题