گرفتن Device Unique ID در React Native


سلام دوستان.در این آموزش متنی قصد داریم یاد بگیریم چجوری unique ID دیوایس های مختلف رو در React Native بگیریم.هر گوشی موبایل در جهان دارای یک unique ID است که ترکیبی از حروف الفبا و اعداد است.unique ID در بحث هایی مثل ارسال نوتیفیکیشن ، login اپلیکیشن و جلوگیری از نصب همزمان چندین اپلیکیشن در یک گوشی استفاده میشه.هر دو گوشی های android و ios از unique ID پشتیبانی میکنند و ما با استفاده از کتابخانه react-native-device-info عملیات مربوط به گرفتن unique ID رو انجام میدیم.
1.باز کردن دایرکتوری پروژه در Command Prompt / Terminal.
2.اجرای دستور npm install —save react–native–device–info@0.11.0 در دایرکتوری پروژه.
3.بعد از نصب کتابخانه باید دستور react–native link react–native–device–info رو اجرا کنیم.
4.ایمپورت کردن کامپوننت های Platform, StyleSheet, View, Text و TouchableOpacity در پروژه.
1 2 3 |
import React, { Component } from 'react'; import { Platform, StyleSheet, View, Text, TouchableOpacity } from 'react-native'; |
5.ایمپورت کردن ماژول react-native-device-info با استفاده از یک متغیر به نام DeviceInfo.
1 |
varv DeviceInfo = require('react-native-device-info'); |
6.ایجاد ()constructor در پروژه و تعریف یک State به نام DeviceID .
1 2 3 4 5 6 7 8 9 10 |
constructor(){ super(); this.state={ DeviceID : ' ' } } |
7.ایجاد یک تابع به نام ()getDeviceID. درون این تابع ما unique device id رو با استفاده از متد ()DeviceInfo.getUniqueID میگیریم و درون state ذخیره میکنیم.این تابع در رویداد onPress دکمه فراخوانی میکنیم.
1 2 3 4 5 6 7 8 9 10 11 |
getDeviceID=()=>{ var id = DeviceInfo.getUniqueID(); this.setState({ DeviceID : id }) } |
8.اضافه کردن View در بلاک render’s return و قرار دادن دو کامپوننت Text و TouchableOpacity .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
render() { return ( <View style={styles.MainContainer}> <Text style={{textAlign: 'center', fontSize: 20, marginBottom: 10}}>{this.state.DeviceID}</Text> <TouchableOpacity onPress={this.getDeviceID} activeOpacity={0.5} style={styles.button} > <Text style={styles.TextStyle}> CLICK HERE TO GET DEVICE UNIQUE ID </Text> </TouchableOpacity> </View> ); |
9.ایجاد استایل سفارشی.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const styles = StyleSheet.create({ MainContainer :{ justifyContent: 'center', alignItems: 'center', flex:1, paddingTop: (Platform.OS == 'ios' ? 20 : ) }, button: { paddingTop: 10, paddingBottom: 10, width: '90%', backgroundColor: '#4CAF50', }, TextStyle:{ color:'#fff', textAlign:'center', } }); |
کد کامل برنامه در فایل App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import React, { Component } from 'react'; import { Platform, StyleSheet, View, Text, TouchableOpacity } from 'react-native'; var DeviceInfo = require('react-native-device-info'); export default class Myproject extends Component { constructor(){ super(); this.state={ DeviceID : ' ' } } getDeviceID=()=>{ var id = DeviceInfo.getUniqueID(); this.setState({ DeviceID : id }) } render() { return ( <View style={styles.MainContainer}> <Text style={{textAlign: 'center', fontSize: 20, marginBottom: 10}}>{this.state.DeviceID}</Text> <TouchableOpacity onPress={this.getDeviceID} activeOpacity={0.5} style={styles.button} > <Text style={styles.TextStyle}> CLICK HERE TO GET DEVICE UNIQUE ID </Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ MainContainer :{ justifyContent: 'center', alignItems: 'center', flex:1, paddingTop: (Platform.OS == 'ios' ? 20 : ) }, button: { paddingTop: 10, paddingBottom: 10, width: '90%', backgroundColor: '#4CAF50', }, TextStyle:{ color:'#fff', textAlign:'center', } }); |
Screenshot in Android device:
Screenshot in iOS device:
دیدگاهتان را بنویسید