0

I have a React Native application running on my iPhone and I would like to change the app configuration (targeting another environment) when a specific configuration profile is installed.

How can I read/access an iOS configuration profile from a React Native application?

ZedTuX
  • 2,859
  • 3
  • 28
  • 58

1 Answers1

0

You can make simple custom module to get that value from iOS native side. Here is the custom module named RNConfig

RNConfig.h

#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"

@interface RNConfig : NSObject<RCTBridgeModule>

@end

RNConfig.m

#import "RNConfig.h"

@implementation RNConfig

RCT_EXPORT_MODULE();

- (NSDictionary *)constantsToExport
{
  NSString* platform_name = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"PlatformName"];
  return @{ @"PlatformName": platform_name };
}

@end

With React Native you can use that module by following code snippets.

const { RNConfig } = require('NativeModules');
let platformName = RNConfig.PlatformName;

This module could be used when you have multiple targets on iOS side. So On React Native side, you have many config values according to every targets.

config/index.js

import { targetA } from './targetA';
import { targetB } from './targetB';

const variables = {
  targetA,
  targetB
};

const { RNConfig } = require('NativeModules');
export default variables[RNConfig.PlatformName];

If your iOS target name is targetA or targetB then you can use the specific configuration setting on React Native side by getting iOS platform name like above.

Anyway, you can get any types of configuration values on iOS side by changing the RNConfig.m file.

awmidas
  • 653
  • 5
  • 13
  • Thank you for your answer! Do you know a documentation page where I can find all the available keys I can get the value from the RNConfig.m file? Or are they coming from the custom profile? – ZedTuX Oct 16 '18 at 07:01
  • It is the dictionary from the **Info.plist** file. https://developer.apple.com/documentation/foundation/nsbundle/1413477-infodictionary?language=objc – awmidas Oct 23 '18 at 05:28
  • Okay so your solution is not about to access a phone profile, right? Or did I misunderstood it? – ZedTuX Oct 23 '18 at 06:46
  • It can be used when you want to use the different environment for every Targets on iOS app. With above method you can access the **Info.plist** file for each target. so if you make the different **Info.plist** file for each target(it is possible and you should do that if you have many targets) and you can access each info file with above method. – awmidas Oct 23 '18 at 08:04
  • Thank you for your comments. I'm talking about iOS Configuration Profiles (like it is described in this article : https://www.howtogeek.com/253325/how-to-create-an-ios-configuration-profile-and-alter-hidden-settings/) – ZedTuX Oct 23 '18 at 08:10
  • Oh, okay. I misunderstood your question. so you mean something like this one? https://www.appconfig.org/ios/ – awmidas Oct 23 '18 at 08:37
  • I mean exactly this : https://developer.apple.com/enterprise/documentation/Configuration-Profile-Reference.pdf – ZedTuX Oct 23 '18 at 12:05
  • I think you can access to configuration profiles with like this. https://developer.apple.com/library/archive/samplecode/sc2279/Introduction/Intro.html – awmidas Oct 23 '18 at 12:26
  • so you can use the **Settings** https://facebook.github.io/react-native/docs/settings – awmidas Oct 23 '18 at 12:27
  • Sorry I don't how Settings is linked to iOS Configuration Profiles – ZedTuX Oct 23 '18 at 13:03