With React Native, how do you get the number of the current phone?
Along the same lines, can I get the user's name?
With React Native, how do you get the number of the current phone?
Along the same lines, can I get the user's name?
You cannot. This cannot be done using iOS public APIs and therefore cannot be done via React Native.
This npm plugin is great to detect phone numbers in a device. react-native-sms-retriever
https://www.npmjs.com/package/react-native-sms-retriever
import SmsRetriever from 'react-native-sms-retriever';
_onPhoneNumberPressed = async () => {
try {
const phoneNumber = await SmsRetriever.requestPhoneNumber();
this.setState({phone: phoneNumber.split('+91')[1]});
} catch (error) {
console.log(JSON.stringify(error));
}}
You can get user's phone number (and much more), using react-native-device-info library.
But first make sure to ask to user for permissions
You can use react-native-sms-retriever which is using HintRequest only available for android
yarn add react-native-sms-retriever
import SmsRetriever from 'react-native-sms-retriever';
import {parsePhoneNumber} from 'awesome-phonenumber';
const openPhoneHintModal=async ()=>{
if (Platform.OS === 'android') {
try {
const phoneNumber = await SmsRetriever.requestPhoneNumber();
console.log(phoneNumber, 'phoneNumber');
const pn = parsePhoneNumber(phoneNumber);
const countryCode = pn.getCountryCode(); //92
const regionCode = pn.getRegionCode(); //PK
const phoneWithoutCountryCode = pn.getNumber('significant'); //342955555X
} catch (error) {
console.log(JSON.stringify(error));
}
}
}
You can use library
react-native-sim-cards-manager v1.0.10
https://github.com/odemolliens/react-native-sim-cards-manager
Comprises of
https://github.com/markneh/react-native-esim
https://github.com/pocesar/react-native-sim-data
for Android API version > 29, change your permission in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" android:maxSdkVersion="29" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
.
It looks like the react-native-sim
library can do this on Android, though as @xanadont mentions, it's not currently possible on iOS.