react-native获取设备信息组件(react-native-device-info)

前端之家收集整理的这篇文章主要介绍了react-native获取设备信息组件(react-native-device-info)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-acquisition-device-information-component-react-native-device-info/

一、组件说明:

该组件同时适配Android和IOS平台。

二、组件介绍

1.首先需要安装组件:npm install react-native-device-info --save

2.IOS初始化:打开Xcode――>右击Libraries――>选择文件到当前项目,进入到node_modules/react-native-device-info――>添加.xcodeproj文件

2.1.在Xcode中点击你的工程名字――>Build Phases――>Link Binary With Libraries――>点击 '+'号按钮,添加libRNDeviceInfo.a文件(如下图所示)

2.2.添加环境变量:$(SRCROOT)/../react-native/React and $(SRCROOT)/../../React 并且修改 recursive

2.3.好了,下面就是基本的用法

importDeviceInfofrom'react-native-device-info'
console.log("DeviceUniqueID",DeviceInfo.getUniqueID());//e.g.FCDBD8EF-62FC-4ECB-B2F5-92C9E79AC7F9
//*notethisisIDFVoniOSsoitwillchangeifallappsfromthecurrentappsvendorhavebeenprevIoUslyuninstalled

console.log("DeviceManufacturer",DeviceInfo.getManufacturer());//e.g.Apple

console.log("DeviceModel",DeviceInfo.getModel());//e.g.iPhone6

console.log("DeviceID",DeviceInfo.getDeviceId());//e.g.iPhone7,2/ortheboardonAndroide.g.goldfish

console.log("DeviceName",DeviceInfo.getSystemName());//e.g.iPhoneOS

console.log("DeviceVersion",DeviceInfo.getSystemVersion());//e.g.9.0

console.log("BundleId",DeviceInfo.getBundleId());//e.g.com.learnium.mobile

console.log("BuildNumber",DeviceInfo.getBuildNumber());//e.g.89

console.log("AppVersion",DeviceInfo.getVersion());//e.g.1.1.0

console.log("AppVersion(Readable)",DeviceInfo.getReadableVersion());//e.g.1.1.0.89

console.log("DeviceName",DeviceInfo.getDeviceName());//e.g.Becca'siPhone6

console.log("UserAgent",DeviceInfo.getUserAgent());//e.g.Dalvik/2.1.0(Linux;U;Android5.1;GoogleNexus4-5.1.0-API22-768x1280Build/LMY47D)

console.log("DeviceLocale",DeviceInfo.getDeviceLocale());//e.gen-US

console.log("DeviceCountry",DeviceInfo.getDeviceCountry());//e.gUS

3.Android的安装:

3.1首先需要修改下Gradle文件

在你的根目录下运行:react-native link react-native-device-info

3.2在MainActivity.java文件中进行注册模块(react-native的版本需要>0.18)

importcom.learnium.RNDeviceInfo.RNDeviceInfo;//<---import

publicclassMainActivityextendsReactActivity{
......

/**
*Alistofpackagesusedbytheapp.Iftheappusesadditionalviews
*ormodulesbesidesthedefaultones,addmorepackageshere.
*/
@Override
protectedList<ReactPackage>getPackages(){
returnArrays.<ReactPackage>asList(
newRNDeviceInfo(),//<------addhere
newMainReactPackage());
}
}

3.3如果你需要在安卓上获取设备的名字,需要修改AndroidManifest.xml配置文件,来获取权限。

...
<uses-permissionandroid:name="android.permission.BLUETOOTH"/>

3.4用法同IOS。

翻译链接:https://github.com/rebeccahughes/react-native-device-info

猜你在找的React相关文章