2

Using a Progressive Web App, can I retrieve the phone number of a mobile phone? I am guessing definitely a no - but maybe if the web browser can ask the user for access the user can give the browser access to the mobile phone number?

This question is specifically for Android phones.

If the webpage could just have a button that said "Copy Phone Number" and it could fill in the corresponding field with the user's phone number, that would be awesome.

3 Answers3

0

you could implement a JavascriptInterface on Android.

look at this answer.

https://es.stackoverflow.com/questions/127574/javascript-interface-android-webview-call-java-function-from-js-onclick

based on this, you could make some code like below:

JavaScriptInterface jsInterface = new JavaScriptInterface();
myWebView.addJavascriptInterface(jsInterface, "Android");

Your method, and based on this other question:

Programmatically obtain the phone number of the Android phone

public class JavaScriptInterface {

    @JavascriptInterface
    public void getPhoneNumber() {
     // Code to get phone number on android
     TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
     String phoneNumber = tMgr.getLine1Number();

    }

}

On Web:

<a href="#" onclick="getPhoneNumber()">getMyNumber</a>

You could send a response to webview also. see this tutorial. it is in Koltin but if you need make that, you could write it on java.

https://medium.com/@elye.project/making-android-interacting-with-web-app-921be14f99d8

Based on this you could make some code like this:

myWebView.loadUrl("javascript: " +"returnPhoneNumber(\"" + phoneNumber + "\")", null);

you would need a function in js called "returnPhoneNumber"

Don't forget put Permission in manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

if you are using a newer Android Version you would need implement permissionrequest. I hope it helps.

Bonestack
  • 81
  • 6
0

Would be awesome indeed but PWS don’t interact with owner info, you can trigger different events that may require permission when it comes to camera, location and pretty much any “invasive” functions that can interact with the hardware and top level software architecture but only native apps have the abilities as far as I know to interact with user info, contacts, etc;

You could create a modal to constantly hit the person for a number when one isn’t present, or create a setup wizard to run on launch which will require the phone number; but programmatically accessing the device and obtaining the number in a PWS/website application is just about not possible for obvious security reasons.

Ilan P
  • 1,602
  • 1
  • 8
  • 13
0

If you're only focusing on Android, check out the Contact Picker API. At the time of writing it only works on Chrome 77+ and Android M or later.

I don't think you can have a "copy phone number" button with this API because the device user has to explicitly select what info to share, but the user could simply select their own number from their phone's contact list and proceed.

slanden
  • 1,199
  • 2
  • 15
  • 35