Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Device Information React-Native iOS

Hey I am trying to get device information from an iPad. I have tried using https://github.com/rebeccahughes/react-native-device-info but after I do pod install it completely break my solution. Id like to be able to at least get the device's name. How can I go about this without using an NPM module or does anyone know of one that works that doest have to pull in extra dependencies?

Thanks!

like image 811
user3637804 Avatar asked Jun 02 '26 12:06

user3637804


1 Answers

This can be done easily using Native Modules, no libraries or dependencies required.

Synchronous version

in your JS:

import { NativeModules} from "react-native"

const DeviceInfo = NativeModules.DeviceInfo;

const deviceName = DeviceInfo.getName();

in iOS: RCTDeviceInfo.h

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

@interface RCTDeviceInfo : NSObject<RCTBridgeModule>

@end

RCTDeviceInfo.m

#import "RCTDeviceInfo.h"

@implementation RCTDeviceInfo

RCT_EXPORT_MODULE(DeviceInfo);

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getName) {
    return [[UIDevice currentDevice] name];
}

@end

Asynchronous version

in your JS:

import { NativeModules} from "react-native"

const DeviceInfo = NativeModules.DeviceInfo;

DeviceInfo.getName().then(deviceName => {
  console.log("Current Device: "+deviceName);
}

in iOS: RCTDeviceInfo.h

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

@interface RCTDeviceInfo : NSObject<RCTBridgeModule>

@end

RCTDeviceInfo.m

#import "RCTDeviceInfo.h"

@implementation RCTDeviceInfo

RCT_EXPORT_MODULE(DeviceInfo);

RCT_EXPORT_METHOD(getName
              getName_resolver:(RCTPromiseResolveBlock)resolve
              getName_rejecter:(RCTPromiseRejectBlock)reject) {
    resolve([[UIDevice currentDevice] name]);
}

@end
like image 119
Jeremy Avatar answered Jun 04 '26 00:06

Jeremy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!