admin管理员组文章数量:1335552
Hey I am trying to get device information from an iPad. I have tried using but after I do pod install it pletely 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?
Thanks!
Hey I am trying to get device information from an iPad. I have tried using https://github./rebeccahughes/react-native-device-info but after I do pod install it pletely 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?
Thanks!
Share Improve this question asked Aug 14, 2018 at 15:06 user3637804user3637804 1772 gold badges2 silver badges10 bronze badges 1- You need a library that uses native code to extract device-related information. – Dan Commented Aug 14, 2018 at 16:26
1 Answer
Reset to default 4This can be done easily using Native Modules, no libraries or dependencies required.
Synchronous version
in your JS:
import { NativeModules} from "react-native"
const DeviceInfo = NativeModules.DeviceInfo;
const deviceName = DeviceInfo.getName();
in iOS: RCTDeviceInfo.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCTDeviceInfo : NSObject<RCTBridgeModule>
@end
RCTDeviceInfo.m
#import "RCTDeviceInfo.h"
@implementation RCTDeviceInfo
RCT_EXPORT_MODULE(DeviceInfo);
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getName) {
return [[UIDevice currentDevice] name];
}
@end
Asynchronous version
in your JS:
import { NativeModules} from "react-native"
const DeviceInfo = NativeModules.DeviceInfo;
DeviceInfo.getName().then(deviceName => {
console.log("Current Device: "+deviceName);
}
in iOS: RCTDeviceInfo.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCTDeviceInfo : NSObject<RCTBridgeModule>
@end
RCTDeviceInfo.m
#import "RCTDeviceInfo.h"
@implementation RCTDeviceInfo
RCT_EXPORT_MODULE(DeviceInfo);
RCT_EXPORT_METHOD(getName
getName_resolver:(RCTPromiseResolveBlock)resolve
getName_rejecter:(RCTPromiseRejectBlock)reject) {
resolve([[UIDevice currentDevice] name]);
}
@end
本文标签: javascriptRetrieving Device Information ReactNative iOSStack Overflow
版权声明:本文标题:javascript - Retrieving Device Information React-Native iOS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396280a2466988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论