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

后端 未结 2 468
生来不讨喜
生来不讨喜 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)
        }
    

提交回复
热议问题