19

With React Native, how do you get the number of the current phone?

Along the same lines, can I get the user's name?

Some Guy
  • 12,768
  • 22
  • 58
  • 86

6 Answers6

13

You cannot. This cannot be done using iOS public APIs and therefore cannot be done via React Native.

xanadont
  • 7,493
  • 6
  • 36
  • 49
  • 1
    Really? Wow... ok. What about their first name, at least? – Some Guy Aug 11 '15 at 02:19
  • The best you can do is "guess" at it by looking at the most likely match in their contacts. See this - http://stackoverflow.com/questions/8000927/how-does-squares-cardcase-app-automatically-populate-the-users-details-from-th/8058188#8058188 – xanadont Aug 11 '15 at 04:43
  • 1
    An other way is use a two factor authentication like whatsapp : https://www.quora.com/How-can-I-implement-sms-verification-like-WhatsApp-in-my-iPhone-application – efx Oct 02 '16 at 19:33
  • 4
    @efx What you linked to is nothing more than asking the user to input their phone number. Which is fine, and really is the industry standard for recording the phone number. But this doesn't address the original question - how to get the user's phone number _programmatically_. – xanadont Oct 03 '16 at 05:00
  • 2
    What about Android? – Noah Passalacqua Apr 12 '17 at 19:39
3

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));
}}
jyotishman saikia
  • 2,079
  • 1
  • 15
  • 11
0

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

Shivam Jha
  • 3,160
  • 3
  • 22
  • 36
0

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));
        }
     
    }
}

enter image description here

Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80
0

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" />  

.

deva11
  • 881
  • 10
  • 25
-1

It looks like the react-native-sim library can do this on Android, though as @xanadont mentions, it's not currently possible on iOS.

Kevin Cooper
  • 5,018
  • 4
  • 37
  • 51