11

I am working on xamarin.forms. I need to get the phone number, if the device is dual sim then I need to get both the numbers. How do I achieve this? There so many questions related to this after referring all I came here.

I created one interface in shard project and implemented the interface in an android project. android.permission.READ_PHONE_NUMBERS

created interface in shared project

public interface IDeviceInfo
{
 string GetMyPhoneNumber();
}

Implemented the interface in android project

public string GetMyPhoneNumber()
{
   try
   {
      TelephonyManager mgr =     
  Android.App.Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
    return mgr.Line1Number;
   }
  catch (Exception ex)
  {
  throw;
  }
}

On one of Button click on shared project

 private void GetPhone_Clicked(object sender, EventArgs e)
 {  
    try
    {

        var data = DependencyService.Get<IDeviceInfo>().GetMyPhoneNumber();
    }
    catch (Exception ex)
    {
          throw;
    }
}

When I am using android 5 it is returning the empty string and if I am using android 6+ it is giving permission error like Java.Lang.SecurityException: getLine1NumberForDisplay: Neither user 10286 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS

How do resolve this in all android version?

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
Sagar Patil
  • 469
  • 9
  • 20

1 Answers1

8

For iOS, this is not possible and even if you somehow get it using CoreTelephony or something else your application would get rejected for apple store deployment with the below issue as stated by Dylan here:

"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."

The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.

Now for Android, you could use the TelephonyManager approach to get the phone number, but in my knowledge, it is not a reliable approach to do so it does not get the phone number from the sim details or something it picks it up from the device information that you enter at the phone's startup an explanation for it is present here

Also, there is an important comment here that seems to be relevant

Actually, not so perfect. Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. It probably still does, as the Settings app still shows that defunct number. Also, there are reports that some SIMs cause this method to return null. That being said, I'm not aware of a better answer.

Even if this somehow is fine with you, you can only get one phone number using this and this phone number would be null most of the times if your user is not configured in the mobile's settings.

Possible solution (only if you ask me)

The way in which both these issues could get solved is, creating a screen something like a pop-up or a page according to your convenience that asks the user himself to enter the phone number if mandatory make a modal page all together

Good luck

Revert in case of queries

Community
  • 1
  • 1
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • Hi @G.Hakim, How to get subscriber id of both sim in xamarin forms? – Sagar Patil Mar 29 '19 at 13:33
  • What do you mean when you say subscriber id? – FreakyAli Mar 29 '19 at 13:40
  • @SagarPatil Sorry i just remembered this, were you able to solve this? – FreakyAli Apr 09 '19 at 09:30
  • Sorry, I was stuck in another important work but from tomorrow again I will start working on this. I have one doubt once the user registers their number first in pop up or something, register mobile number should be that device. Whenever the customer opens the app that SIM should be inside the device. How to achieve this? – Sagar Patil Apr 09 '19 at 09:42
  • Maybe you can do this in Android but not on iOS, your sim is inaccessible in various ways as sandbox would not allow that, could you explain why are you looking for this? – FreakyAli Apr 13 '19 at 11:07