【问题标题】:How to hide API keys in GitHub for iOS (SWIFT) projects?如何在 GitHub for iOS (SWIFT) 项目中隐藏 API 密钥?
【发布时间】:2015-08-28 11:22:32
【问题描述】:

您好,我正在尝试在 GitHub 上发布一个 iOS (SWIFT) 个人项目,但我害怕与所有人共享我的私有 API 密钥和秘密。

我正在使用解析,所以我的 AppDelegate 中有这样的内容:

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

我想隐藏“mySecretApplicationId”和“mySecretClientKey”,我的项目中是否有可以放置这些变量的私有位置或目录?

谢谢!

【问题讨论】:

    标签: ios swift api parsing apple-watch


    【解决方案1】:

    将它们放入您添加到.gitignore 文件的配置文件中。签入每个开发人员都可以用来创建自己的配置的示例配置文件。

    【讨论】:

      【解决方案2】:

      您可以使用.plist 文件来存储所有重要密钥。将此文件放入您的.gitignore 文件中非常重要。

      在您的情况下,您需要像这样设置您的 keys.plist 文件:

      并在您的 AppDelegate 中使用它,如下所示:

          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 更新:

       if let path = Bundle.main.path(forResource: "Keys", ofType: "plist") {
              keys = NSDictionary(contentsOfFile: path)
          }
      

      【讨论】:

      • 还应该注意的是,即使你这样做了,这些键仍然会在你的 git 历史记录中。要解决这个问题,只需生成一开始从未在存储库中的新 API 密钥。
      • 如何防止它们被包含在“复制捆绑资源”中?最终用户难道不能只显示包内容并查看 Resources 文件夹中的 keys.plist 文件吗?
      【解决方案3】:

      如果您想在没有密钥的情况下共享您的项目,那么:

      1. 添加键(根据您的喜好 - 枚举、结构,甚至是对象/单例)
      struct Keys {
          static let sandboxToken = "Tpk_hh43nneu3jwsu3u"
          static let productionToken = "pk_b5h4uend8ejwnw8"
      }
      
      1. 在您的代码中添加以下代码:
      extension APIManager {
          enum Environment {
      
              case sandbox, production
      
              var apiKey: String {
                  switch self {
                  case .sandbox:
                      return Keys.iexSandboxToken // <- Here
      
                  case .production:
                      return Keys.iexProductionToken // <- Here
                  }
              }
          }
      }
      

      或者如果你想处理可选的,那么你可以添加类似的东西:

      struct Keys {
          static let sandboxToken: String? = "Tpk_hh43nneu3jwsu3u"
          static let productionToken: String?
      }
      

      并在使用时添加断言

              var apiKey: String {
                  switch self {
                  case .sandbox:
                      guard let token = Keys.iexSandboxToken else {
                          assertionFailure("Please fill the tokent in Keys.swift")
                          return "anything you want"
                      }
                      return token
      
                  case .production:
                      guard let token = Keys.iexProductionToken else {
                          assertionFailure("Please fill the tokent in Keys.swift")
                          return "anything you want"
                      }
                      return token
                  }
              }
      

      所以,在生产中,它会失败。

      1. 将其添加到 .gitignore。因此,您的密钥已隐藏。

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        • 2014-03-23
        • 2021-04-07
        • 2021-01-27
        • 2020-04-15
        • 2020-05-12
        相关资源
        最近更新 更多