Reputation: 13568
I want access to the device name (from UIDevice) with React Native. Is there a good way to do that?
Upvotes: 3
Views: 4319
Reputation: 8483
You need to make a Native Modules (iOS). Here are the steps:
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