Showing posts with label IOS Bridging. Show all posts
Showing posts with label IOS Bridging. Show all posts

April 22, 2019

How to read and get value from Info.plist file using react native in IOS app

Info.plist file is a very important file which handles all the configuration for an app. We can use this file to have some custom information specific to app which can be read when app is getting loaded.

To read the file we need to make a bridge component that allow us to get values from the plist file. Because app cannot directly read the file until the app is developed in native language.

Info.plist is having data in JSON format, which can be read as key, value pair.

First, we need to create a custom class (Objective-C) that will read info.plist file and send the response back to app.


#import "AppConfig.h"
@interface AppConfig()
@end

@implementation AppConfig
RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(getPListValue:(NSString *)param callback:(RCTResponseSenderBlock)callback)
{
  NSString* paramValue = [[[NSBundle mainBundle] infoDictionary] valueForKey:param];
  if(paramValue == nil)
    paramValue = @"";
  callback(@[[NSNull null], paramValue]);
}
@end


Using NativeModules we can call this function and get the value of any keys in react native. Import NativeModules from react-native and directly call the method as below:


import { NativeModules } from 'react-native';
NativeModules.AppConfig.getPListValue("keyName", (error, keyValue) => {
  console.log(keyValue);
});

On running above code, it will log the key value for the specific key you have given in code as keyName.