1. 程式人生 > >React Native獲取裝置資訊元件

React Native獲取裝置資訊元件

轉載 https://www.jianshu.com/p/907b003835dc


本文原創首發於公眾號:ReactNative開發圈,轉載需註明出處。

這次介紹的獲取移動裝置資訊的元件名叫:react-native-device-info,相容IOS和安卓雙平臺,可以獲取裝置ID、裝置品牌、裝置型號、IP以及APP版本號等資訊。是一個應用很廣泛的基礎元件。

安裝

npm install --save react-native-device-info
注:如果React Naitve的版本大於0.47,那麼需要使用>=0.11版本的react-native-device-info元件

因為涉及到原生程式碼,所以在執行完install後,需要執行link命令
react-native link react-native-device-info

使用示例

  import DeviceInfo from 'react-native-device-info';

  const device = {};
  device.DeviceID = deviceInfo.getUniqueID();
  device.UserAgent = deviceInfo.getUserAgent();
  device.DeviceBrand = deviceInfo.getBrand();
  device.DeviceModel = deviceInfo.getModel();
  device.SystemVersion = deviceInfo.getSystemVersion();
  device.AppVersion = deviceInfo.getVersion();
  device.AppReadableVersion = deviceInfo.getReadableVersion();

該元件使用也很方便,直接import後,直接呼叫相應的方法即可。但是有一個方法比較特殊就是isPinOrFingerprintSet方法,需要使用非同步的方式來調。程式碼如下:

import DeviceInfo from 'react-native-device-info';

DeviceInfo.isPinOrFingerprintSet()(isPinOrFingerprintSet => {
  if (!isPinOrFingerprintSet) {
    ...
  }
}

方法列表

image.png
image.png
image.png

元件地址

該元件的詳細說明和示例可以進入GitHub檢視。
GitHub地址:https://github.com/rebeccahughes/react-native-device-info

舉手之勞關注我的微信公眾號:ReactNative開發圈



作者:forrest23
連結:https://www.jianshu.com/p/907b003835dc
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。