Reputation: 5061
I need to get a unique ID of a device that is always the same even when installing and uninstalling and reinstalling. Is there anything that is accessible anymore to accomodate this requirement?
Device name is now out the window with iOS 16 and it seems like serial number is not possible either. Is there something that can be unique to a device?
My app is not distributed through the app store either.
Thanks
Upvotes: 4
Views: 17797
Reputation: 1
you have three options here:
1)Get the unique device ID
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import DeviceInfo from 'react-native-device-info';
const App = () => {
const [uniqueId, setUniqueId] = useState('');
useEffect(() => {
const fetchUniqueId = async () => {
const id = await DeviceInfo.getUniqueId();
setUniqueId(id);
};
fetchUniqueId();
}, []);
return (
<View>
<Text>Unique Device ID: {uniqueId}</Text>
</View>
);
};
export default App;
2)UUID Generation and Storage:
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { v4 as uuidv4 } from 'uuid';
const App = () => {
const [uuid, setUuid] = useState('');
useEffect(() => {
const fetchOrGenerateUuid = async () => {
try {
let storedUuid = await AsyncStorage.getItem('uuid');
if (!storedUuid) {
storedUuid = uuidv4();
await AsyncStorage.setItem('uuid', storedUuid);
}
setUuid(storedUuid);
} catch (error) {
console.error('Error fetching or generating UUID:', error);
}
};
fetchOrGenerateUuid();
}, []);
return (
<View>
<Text>UUID: {uuid}</Text>
</View>
);
};
export default App;
3)Instance ID (Firebase):
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import messaging from '@react-native-firebase/messaging';
const App = () => {
const [instanceId, setInstanceId] = useState('');
useEffect(() => {
const fetchInstanceId = async () => {
try {
const id = await messaging().getToken();
setInstanceId(id);
} catch (error) {
console.error('Error getting instance ID:', error);
}
};
fetchInstanceId();
}, []);
return (
<View>
<Text>Instance ID: {instanceId}</Text>
</View>
);
};
export default App;
Upvotes: 0
Reputation: 1874
You can use react-native-device-info, in that there are method called getDeviceToken() and getUniqueId() try to use that.
getUniqueId() gives you UUID of device.
https://www.npmjs.com/package/react-native-device-info#getdeviceid
Upvotes: 6