我想使用 React Native 访问设备名称(来自 UIDevice)。有什么好的方法吗?
最佳答案
你需要做一个Native Modules (iOS) .以下是步骤:
- 制作用于获取设备名称的原生 iOS 组件
- 在JS中使用
1.制作组件
// 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.在JS中使用
var Device = require('react-native').NativeModules.Device;
Device.deviceName( (name) => {
console.log(name)
});
关于ios - 如何使用 React Native 从 UIDevice 获取设备名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31933385/