We use cocoapods-keys
to store secrets (similar to a .env
file). In order to expose these keys to our react-native components we must do a fair bit of setup.
Links to examples below come from this commit which adds a key for the Stripe API.
This is the extent of cocoapods-keys
official setup, and after this you could set the key via pod keys set <NAME>
or pod install
... but we have more to do.
Example/Podfile
plugin 'cocoapods-keys', {
:target => 'Emission',
:keys => [
'ArtsyAPIClientSecret', # Authing to the Artsy API
'ArtsyAPIClientKey', #
+ 'StripePublishableKey',
]
}
We'll need to update the initWithUserId...
function, expose the new key as a property and add it to constantsToExport
.
// ENV Variables
+ @property (nonatomic, copy, readonly, nullable) NSString *stripePublishableKey;
# ...
- (instancetype)initWithUserID:(NSString *)userID
authenticationToken:(NSString *)token
sentryDSN:(nullable NSString *)sentryDSN
+ stripePublishableKey:(NSString *)stripePublishableKey
googleMapsAPIKey:(nullable NSString *)googleAPIKey
gravityURL:(NSString *)gravity
metaphysicsURL:(NSString *)metaphysics
userAgent:(NSString *)userAgent;
- (NSDictionary *)constantsToExport
{
return @{
# Add this line...
+ @"stripePublishableKey": self.stripePublishableKey ?: @"",
# ...
};
}
- (instancetype)initWithUserID:(NSString *)userID
authenticationToken:(NSString *)token
sentryDSN:(NSString *)sentryDSN
+ stripePublishableKey:(NSString *)stripePublishableKey
googleMapsAPIKey:(NSString *)googleAPIKey
gravityURL:(NSString *)gravity
metaphysicsURL:(NSString *)metaphysics
userAgent:(nonnull NSString *)userAgent
{
self = [super init];
_userID = userID.copy;
# ... More copies...
+ _stripePublishableKey = stripePublishableKey.copy;
return self;
}
Emission
is now exposed along with its configured keys via react-native
's NativeModules
.
import { NativeModules } from "react-native"
const Emission = NativeModules.Emission || {}
// ... other setup ...
stripe.setOptions({
publishableKey: Emission.stripePublishableKey,
})
oss:
@echo "Installing Cocoa Dependencies"
cd Example && bundle exec pod keys set ArtsyAPIClientKey "e750db60ac506978fc70"
# Hidden keys by convention are set to a single dash
cd Example && bundle exec pod keys set StripePublishableKey "-"
# ...And so on