Simple way to get the bundle identifier at runtime in React Native?

Viewed 12438

We've hit a problem that could be solved if we know the bundle identifier at run time in our RN app. We've seen a few libraries but would rather not include a whole library to do this one thing. Is there a simple way to get this information in the code?

Thanx

4 Answers

on mac you can see it inside general Tab enter image description here

you can also use

import DeviceInfo from "react-native-device-info";

console.log("@ device   ",DeviceInfo.getBundleId())

It seems like libraries like react-native-device-info use native iOS and Android code in order to return the applications bundle identifier which means that react-native doesn't provide that in javascript. You could use React Native's native-modules API to send the bundle id from Java or Objective C to javascript (they have a section in the linked docs on how to export constant values).

Also here are some snippets from react-native-device-info on how they retrieved the bundle identifier.

On Android (RNDeviceModule.java):

String bundleId = this.reactContext.getPackageName();

On iOS (RNDeviceInfo.m) - keep in mind idk objective-c so this snippet might not work:

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"] ?: [NSNull null]

Also if you are using expo currently, then you can access the app.json file with Expo.Constants:

import { Constants } from 'expo';
// this will only work if you specify the bundleIdentifier in app.json
let bundleId = Constants.manifest.ios.bundleIdentifier;

Hope this mess of a post somewhat helps! Honestly, it might just be worth it to use a library but it's up to you.

You can get this from DeviceInfo.getVersion()

replace getVersion to the following list according to your need

export function getUniqueID(): string;
export function getManufacturer(): string;
export function getDeviceId(): string;
export function getSystemName(): string;
export function getSystemVersion(): string;
export function getBundleId(): string;
export function getBuildNumber(): string;
export function getVersion(): string;
export function getDeviceName(): string;

If you are using react-native-fs in your app anyway, you can do the following slightly ugly hack to get your bundle Id:

import RNFS from 'react-native-fs';

getAppBundleIdAndroid = () => 
    RNFS.DocumentDirectoryPath.replace(/.+\/(.+?)\/files/, '$1');

}

Related