Some Guy
Some Guy

Reputation: 13568

How can I get the device name from UIDevice with React Native?

I want access to the device name (from UIDevice) with React Native. Is there a good way to do that?

Upvotes: 3

Views: 4319

Answers (1)

Kostiantyn Koval
Kostiantyn Koval

Reputation: 8483

You need to make a Native Modules (iOS). Here are the steps:

  • Make native iOS component for getting device name
  • Use it in JS

1. Make Component

//  Device.h
@import UIKit;
#import "RCTBridgeModule.h"

@interface Device : NSObject <RCTBridgeModule>

@end

//  Device.m
#import "Device.h"

@implementation Device
RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(deviceName:(RCTResponseSenderBlock)callback) {
  NSString *deviceName = [[UIDevice currentDevice] name];
  callback(@[deviceName]);
}
@end

2. Use it in JS

var Device = require('react-native').NativeModules.Device;
Device.deviceName( (name) => {
  console.log(name) 
});

Upvotes: 4

Related Questions