0

I want to get the user's phone number using .Net Maui, only in Android for a while. I saw some people saying its only possible if the user set his own phone number in Settings -> About Phone -> Status -> My phone Number.

but its not the better way of doing this. I will appreciate any kind of help, thanks for reading this.

  • In xamarin android you could use TelephonyManager (you could try to make the same thing in maui), IOS protect that data, so the easy way (INMO) to do it is to ask user's phone number one time (like telegram for example) – Leandro Toloza Oct 20 '22 at 13:34
  • *"I want to get the user's phone number"* - This is assuming that the phone actually knows. It requires that the phone number(s) is explicitly stored on the SIM, which frequently isn't the case. A mobile phone need not know its phone number to function. – IInspectable Oct 21 '22 at 17:05

1 Answers1

1

I have an answer for this in Xamarin Forms. Let's do the same for Maui too.

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
FreakyAli
  • 13,349
  • 3
  • 23
  • 63