Securing Your Application Keys

secure

CocoaPods-Keys is a way of removing API keys and secrets from your code. Securing application keys can be difficult, CocoaPods-Keys abstracts away this difficulty for you. When you add keys and associated values using CocoaPods-Keys, the key names are stored in ~/.cocoapods/keys/ and the associated values in the OS X keychain.

Installing CocoaPods-Keys

CocoaPods-Keys is a Ruby gem and can be installed by running:

$ gem install cocoapods-keys

In Ruby, it is common to use Bundler to manage gem dependencies. If you don’t already have Bundler installed you can install it by running:

$ gem install bundler

Once you have Bundler installed, you can create a Gemfile to manage dependencies by running:

$ bundle init

Open the Gemfile in a text editor and edit as follows:

source "https://rubygems.org"

gem 'cocoapods-keys'

To install CocoaPods-Keys you can run:

$ bundle install

Setting up Keys

With CocoaPods-Keys installed, open up your Podfile and add the following code:

plugin 'cocoapods-keys', {
  :project => 'ProjectName',
  :keys => [
    "SampleKey"
  ]
}

You define what keys you want inside your Podfile and Keys detects whether the key has been set yet or not, if not you will be prompted to provide the key value when you run `pod install`.

Using Keys

When CocoaPods-Keys does its stuff it generates an Objective-C class and stores it in ~/.cocoapods/keys/. To use the keys in your project you first need to import the keys module and you will then be able to access your keys as follows:

import Keys

let keys = ProjectNameKeys()
print("key value \(keys.SampleKey)")
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *