问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/55391679/how-to-get-phone-number-in-xamarin-forms