检索设备信息React-Native iOS [英] Retrieving Device Information React-Native iOS
问题描述
嘿我正试图从iPad获取设备信息。我尝试过使用 https://github.com/rebeccahughes/react-native-device-信息但在我安装pod之后它完全破坏了我的解决方案。我希望能够至少获得设备的名称。如何在不使用NPM模块的情况下解决这个问题,或者是否有人知道哪个工作必须引入额外的依赖关系?
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?
谢谢!
推荐答案
这可以通过本机模块,不需要库或依赖项。
This can be done easily using Native Modules, no libraries or dependencies required.
同步版
在你的JS中:
import { NativeModules} from "react-native"
const DeviceInfo = NativeModules.DeviceInfo;
const deviceName = DeviceInfo.getName();
:
RCTDeviceInfo.h
in iOS: RCTDeviceInfo.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCTDeviceInfo : NSObject<RCTBridgeModule>
@end
RCTDeviceInfo.m
RCTDeviceInfo.m
#import "RCTDataLogger.h"
@implementation RCTDeviceInfo
RCT_EXPORT_MODULE(DeviceInfo);
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getName) {
return [[UIDevice currentDevice] name];
}
@end
异步版
在你的JS中:
import { NativeModules} from "react-native"
const DeviceInfo = NativeModules.DeviceInfo;
DeviceInfo.getName().then(deviceName => {
console.log("Current Device: "+deviceName);
}
在iOS中:
RCTDeviceInfo.h
in iOS: RCTDeviceInfo.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCTDeviceInfo : NSObject<RCTBridgeModule>
@end
RCTDeviceInfo.m
RCTDeviceInfo.m
#import "RCTDataLogger.h"
@implementation RCTDeviceInfo
RCT_EXPORT_MODULE(DeviceInfo);
RCT_EXPORT_METHOD(getName
getName_resolver:(RCTPromiseResolveBlock)resolve
getName_rejecter:(RCTPromiseRejectBlock)reject) {
resolve([[UIDevice currentDevice] name]);
}
@end
这篇关于检索设备信息React-Native iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!