I want access to the device name (from UIDevice) with React Native. Is there a good way to do that?
Asked
Active
Viewed 4,034 times
3
-
Show this link : http://stackoverflow.com/questions/1100127/how-do-you-get-an-iphones-device-name – Mihir Oza Aug 11 '15 at 04:45
-
I understand it's accessible from UIDevice, I'm wondering if there's a React Native library or way to expose UIDevice. – Some Guy Aug 11 '15 at 05:25
-
NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]); NSLog(@"name: %@", [[UIDevice currentDevice] name]); NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]); NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]); NSLog(@"model: %@", [[UIDevice currentDevice] model]); NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]); – Mihir Oza Aug 11 '15 at 05:33
-
Your comment is unhelpful because it ignores the React Native part of the question. – Some Guy Aug 11 '15 at 06:19
-
Here is a component the gives some info about the device, you could extend it https://github.com/GertjanReynaert/react-native-device or build your own one native component – Kostiantyn Koval Aug 11 '15 at 08:51
1 Answers
4
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)
});

Kostiantyn Koval
- 8,407
- 1
- 45
- 58
-
Device is undefined in JS and i get this error: `Cannot read property 'deviceName' of undefined` – Alireza Akbari Jun 10 '18 at 07:22