Search code examples
meteormeteor-accounts

Meteor - Detect Sim Phone Number


Is there a way to detect the SIM phone number on a mobile device while using Meteor? Moreover, what is the correct behavior to have and precautions to make to log users using their phone number (like in Whatsapp or Viber for example)?

Thank you in advance.


Solution

  • It depends if it's a web app or a hybrid application

    First case, web app

    If you're talking about a Meteor application on the web which you will access with your phone's browser, then the answer is NO (see atmd's answer)

    Second case, hybrid app

    If you're talking about a hybrid app built with Cordova i.e. meteor run android, then YES it's possible

    If you want to go quicker, I coded an example Meteor app: https://github.com/Erdou/meteor-cordova-phonenumber-example

    1. Add Android platform (if not done yet)

    $ meteor add-platform android   
    

    2. Add a phone number plugin for Cordova

    There is no plugin ready for Meteor, so you need to it manually. We will use this one here: https://github.com/rotorgames/phonegap-telephonenumber-plugin

    • Get the .git path (near Github's Clone button): https://github.com/rotorgames/phonegap-telephonenumber-plugin.git
    • Get the latest commit hash. In Github, click on the Latest commit number, and this number will fully show: ca0f55481fe8ea61e7857e96c1324591596271ee
    • The command will be: $ meteor add cordova:[a plugin name]@[.git path]#[commit hash]

    In our case:

    $ meteor add cordova:cordova-plugin-phonenumber@https://github.com/rotorgames/phonegap-telephonenumber-plugin.git#ca0f55481fe8ea61e7857e96c1324591596271ee
    

    3. Use it in your app

    if (Meteor.isCordova) {
        TelephoneNumber.get(function(result) {
            console.log('Phone number: ' + result.line1Number);
          }, function() {
            console.log('Error. Do the phone have this feature? (Settings > About Phone > SIM > Number)');
          });
      }
    

    4. Run the hybrid app

    $ meteor run android
    

    It should work! :)

    A final case as a suggestion

    As atmd said, using this feature is not recommended. If you want to know user's phone number, the best will be the implement the classical SMS sending / confirmation used by Viber or WhatsApp as you kind of guessed it.

    I didn't test it, but there is even a Meteor package for that: https://github.com/DispatchMe/meteor-accounts-sms