【React Native】获取设备信息

一、获取方案

使用开源的第三方组件react-native-device-info,该组件适用于iOS和Android双平台。

npm地址:https://www.npmjs.com/package/react-native-device-info

二、组件使用

在ReactNative项目中下载第三方组件依赖包, 我们一般都是使用命令行来执行下载: 

进入项目执行:npm install –save react-native-device-info 或者 yarn add react-native-device-info

2.1 iOS端:

(1)首先在xcode中进行初始化

打开xcode,找到自己的项目->Libraries文件夹,选择Add Files to '项目名',然后找到当前项目目录/node_modules/react-native-device-info文件夹,找到'RNDe'viceInfo.xcodeproj'文件,然后点击Add.

(2)再在xcode中加载内库

找到项目名字 ----> Build Phases ---> Link Binary With Libraries, 点击‘+’按钮,添加‘libRNDeviceInfo.a’库。

(2)继续添加环境变量

(SRCROOT)/../react-native/Reactand

(SRCROOT)/../../React

并修改recursive.

2.2 Android端:

(1)首先需要修改下Gradle文件

在项目根目录下运行命令react-native link react-native device-info即可,成功了会增加如下两行代码:

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

    import com.learnium.RNDeviceInfo.RNDeviceInfo; // <— import
    public class MainActivity extends ReactActivity { 
    …… 
    /** 
     * A list of packages used by the app. If the app uses additional views 
     * or modules besides the default ones, add more packages here. 
     */ 
    @Override 
    protected List getPackages() { 
        return Arrays.asList( 
            new RNDeviceInfo(), // <—— add here 
            new MainReactPackage()); 
        } 
    }

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

在android设备中获取以下权限: uses-permission android:name=”android.permission.BLUETOOTH”

三、基本用法

Examples

    const apiLevel = DeviceInfo.getAPILevel();
 
    // iOS: ?
    // Android: 25
    // Windows: ?

四、API

MethodReturn TypeiOSAndroidWindowsSince
getAPILevel()number0.12.0
getApplicationName()string0.14.0
getBatteryLevel()Promise<number>0.18.0
getBrand()string0.9.3
getBuildNumber()string?
getBundleId()string?
getCarrier()string0.13.0
getDeviceCountry()string0.9.0
getDeviceId()string0.5.0
getDeviceLocale()string0.7.0
getDeviceName()string?
getFirstInstallTime()number0.12.0
getFontScale()number0.15.0
getFreeDiskStorage()number0.15.0
getIPAddress()Promise<string>0.12.0
getInstallReferrer()string0.19.0
getInstanceID()string?
getLastUpdateTime()number0.12.0
getMACAddress()Promise<string>0.12.0
getManufacturer()string?
getMaxMemory()number0.14.0
getModel()string?
getPhoneNumber()string0.12.0
getReadableVersion()string?
getSerialNumber()string0.12.0
getSystemName()string?
getSystemVersion()string?
getTimezone()string?
getTotalDiskCapacity()number0.15.0
getTotalMemory()number0.14.0
getUniqueID()string?
getUserAgent()string0.7.0
getVersion()string?
is24Hour()boolean0.13.0
isEmulator()boolean?
isPinOrFingerprintSet()(callback)boolean0.10.1
isTablet()boolean?

参考链接:https://www.npmjs.com/package/react-native-device-info

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhangKui_c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值