├── LICENSE.md ├── README.md ├── RNEnvironmentManagerIOS ├── RNEnvironmentManagerIOS.h └── RNEnvironmentManagerIOS.m ├── index.js └── package.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joseph P. Ferraro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## react-native-env 2 | 3 | Access the iOS environment from React Native. 4 | 5 | ### Installation 6 | 7 | 1. `npm install react-native-env` 8 | 2. In XCode's "Project navigator", right click on project's name ➜ `Add Files to <...>` 9 | 3. Go to `node_modules` ➜ `react-native-env` ➜ add `RNEnvironmentManagerIOS` folder 10 | 4. Add an `environment.plist` to your project containing any key-values to be accessed at runtime. 11 | 5. Compile and have some environment 12 | 13 | ### Per-environment Config 14 | 15 | Follow the excellent, long tutorial [Migrating iOS App Through Multiple Environments](http://www.blackdogfoundry.com/blog/migrating-ios-app-through-multiple-environments/) to derive `environment.plist` at build-time, based on the selected Xcode scheme. 16 | 17 | ### Usage 18 | 19 | ```javascript 20 | var EnvironmentManager = require('react-native-env'); 21 | 22 | // read an environment variable from React Native 23 | EnvironmentManager.get('SOME_VARIABLE') 24 | .then((val) => { 25 | console.log('value of SOME_VARIABLE is: ', val); // => value of SOME_VARIABLE is: MY_VALUE 26 | }) 27 | .catch((err) => { 28 | console.error('womp womp: ', err.message); 29 | }); 30 | 31 | // Read an environment variable synchronously. 32 | // Downside of this approach is that if environment.plist 33 | // changes during runtime, those changes will not be reflected 34 | // by getSync. 35 | var val = EnvironmentManager.getSync('SOME_VARIABLE'); 36 | console.log('value of SOME_VARIABLE is: ', val); 37 | ``` 38 | 39 | ### Roadmap 40 | 41 | - Not sure, what do we need? 42 | 43 | PR's welcome! 44 | -------------------------------------------------------------------------------- /RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.h: -------------------------------------------------------------------------------- 1 | // RNCookieManagerIOS.h 2 | #import "RCTBridgeModule.h" 3 | 4 | @interface RNEnvironmentManagerIOS : NSObject 5 | 6 | @end -------------------------------------------------------------------------------- /RNEnvironmentManagerIOS/RNEnvironmentManagerIOS.m: -------------------------------------------------------------------------------- 1 | #import "RNEnvironmentManagerIOS.h" 2 | #import "RCTConvert.h" 3 | 4 | @implementation RNEnvironmentManagerIOS 5 | 6 | RCT_EXPORT_MODULE() 7 | 8 | RCT_EXPORT_METHOD(get:(NSString *)name callback:(RCTResponseSenderBlock)callback) { 9 | @try { 10 | NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; 11 | if ([env objectForKey:name]) { 12 | callback(@[[NSNull null], env[name]]); 13 | } else { 14 | callback(@[[NSNull null], [NSNull null]]); 15 | } 16 | } 17 | @catch (NSException *exception) { 18 | callback(@[exception.reason, [NSNull null]]); 19 | } 20 | } 21 | 22 | RCT_EXPORT_METHOD(getAll:(RCTResponseSenderBlock)callback) { 23 | @try { 24 | NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; 25 | callback(@[[NSNull null], env]); 26 | } 27 | @catch (NSException *exception) { 28 | callback(@[exception.reason, [NSNull null]]); 29 | } 30 | } 31 | 32 | - (NSDictionary *)constantsToExport 33 | // Include environment variables so they are available without using a promise. 34 | { 35 | NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]]; 36 | return env; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var NativeModules = require('react-native').NativeModules; 2 | var RNEnvironmentManagerIOS = NativeModules.RNEnvironmentManagerIOS; 3 | 4 | module.exports = { 5 | get(name) { 6 | return new Promise((resolve, reject) => { 7 | RNEnvironmentManagerIOS.get(name, (err, res) => { 8 | if (err) { 9 | reject(err); 10 | } else { 11 | resolve(res); 12 | } 13 | }); 14 | }); 15 | }, 16 | 17 | getAll() { 18 | return new Promise((resolve, reject) => { 19 | RNEnvironmentManagerIOS.getAll((err, res) => { 20 | if (err) { 21 | reject(err); 22 | } else { 23 | resolve(res); 24 | } 25 | }); 26 | }); 27 | }, 28 | 29 | getSync(name) { 30 | if (!RNEnvironmentManagerIOS[name]) console.error('Could not get env var', name, '. Has it been defined in environment.plist?'); 31 | return RNEnvironmentManagerIOS[name]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-env", 3 | "version": "0.0.6", 4 | "description": "Environment manager for react native", 5 | "main": "index.js", 6 | "author": "@joeferraro", 7 | "license": "MIT" 8 | } 9 | --------------------------------------------------------------------------------