前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >react-native获取设备信息组件(react-native-device-info)

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

作者头像
IT架构圈
发布2018-06-01 10:44:41
2.1K0
发布2018-06-01 10:44:41
举报
文章被收录于专栏:IT架构圈IT架构圈

一、组件说明:

该组件同时适配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)/../reactnative/Reactand(SRCROOT)/../../React 并且修改 recursive

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

代码语言:javascript
复制
import DeviceInfo from 'react-native-device-info'
console.log("Device Unique ID", DeviceInfo.getUniqueID());  // e.g. FCDBD8EF-62FC-4ECB-B2F5-92C9E79AC7F9
// * note this is IDFV on iOS so it will change if all apps from the current apps vendor have been previously uninstalled
console.log("Device Manufacturer", DeviceInfo.getManufacturer());  // e.g. Apple
console.log("Device Model", DeviceInfo.getModel());  // e.g. iPhone 6
console.log("Device ID", DeviceInfo.getDeviceId());  // e.g. iPhone7,2 / or the board on Android e.g. goldfish
console.log("Device Name", DeviceInfo.getSystemName());  // e.g. iPhone OS
console.log("Device Version", DeviceInfo.getSystemVersion());  // e.g. 9.0
console.log("Bundle Id", DeviceInfo.getBundleId());  // e.g. com.learnium.mobile
console.log("Build Number", DeviceInfo.getBuildNumber());  // e.g. 89
console.log("App Version", DeviceInfo.getVersion());  // e.g. 1.1.0
console.log("App Version (Readable)", DeviceInfo.getReadableVersion());  // e.g. 1.1.0.89
console.log("Device Name", DeviceInfo.getDeviceName());  // e.g. Becca's iPhone 6
console.log("User Agent", DeviceInfo.getUserAgent()); // e.g. Dalvik/2.1.0 (Linux; U; Android 5.1; Google Nexus 4 - 5.1.0 - API 22 - 768x1280 Build/LMY47D)
console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US
console.log("Device Country", DeviceInfo.getDeviceCountry()); // e.g US

3.Android的安装:

3.1首先需要修改下Gradle文件

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

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

代码语言:javascript
复制
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<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new RNDeviceInfo(), // <------ add here
        new MainReactPackage());
    }
}

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

代码语言:javascript
复制
...
<uses-permission android:name="android.permission.BLUETOOTH"/>

3.4用法同IOS。

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档