How to get the phone number in Ionic using Cordova or phonegap

Asked

Viewed 1,709 times

0

I’m developing an app and I need to get the phone number. I did a lot of research online, and I couldn’t find much. One of the examples I found was this: Cordova-plugin-yes but I couldn’t get him to return the number. Here’s an example of my code:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('mainController', ['$scope','$cordovaGeolocation','$interval',function ($scope,$cordovaGeolocation,$interval) {

  $scope.geolocation = function() {
    $interval(function(){
        var geoSettings = {frequency: 30000, timeout: 100000,enableHighAccuracy: false};
        var geo = $cordovaGeolocation.getCurrentPosition(geoSettings);
        geo.then(function (position) {
                $scope.latitude = position.coords.latitude;
                $scope.longitude = position.coords.longitude;
                console.log($scope.latitude)
                console.log($scope.longitude)
            },
            function error(err) {
                $scope.errors = err;
            }
        );
    },3000);
  }
}]);
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">

    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="js/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>
    <script type="text/javascript">
 document.addEventListener("deviceready",onDeviceReady,false);
 function onDeviceReady(){
 window.plugins.sim.getSimInfo(successCallback, errorCallback);
 }
 function successCallback(result) {
 document.getElementById("simInfo").innerHTML=JSON.stringify(result);
 }
function errorCallback(error) {
 document.getElementById("simInfo").innerHTML=JSON.stringify(result);
 }
 </script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Ionic Blank Starter</h1>
    </ion-header-bar>
    <ion-content ng-controller="mainController">
    <button class="button button-full button-assertive" ng-click="geolocation()">
    Localização
    </button>
    <sapn>Latitude: {{latitude}} </sapn>
    <br>
    <sapn>Longitude: {{longitude}} </sapn>
    <br>
    <sapn>Info: {{infoPhone}} </sapn>
    <p id="simInfo"></p>
    </ion-content>
  </ion-pane>
  </body>
</html>

2 answers

1

According to her own plugin documentation, the content representing the phone number is unreliable and can return a blank string (or null):

"Notice: the content of phoneNumber is unreliable (see this, this, and this article). Sometimes phoneNumber is only an Empty string."

Also, in some cases, it can even return an old number, which was previously stored on the SIM card.

From what I read, the fact that this information is present in the SIM card or not (and whether it is updated or not), goes to the operator - some store this information in the SIM card, and others not (doing everything via network).

In my tests I did on a TIM cell phone, returned several information, but did not return the number of the device (and I asked for permission right, as has to be done from version 23). I want to soon test the same code in other cell phones and with other operators' chips.

  • After some tests, I saw that this was msm. At the time testing on other phones worked. That is, for some cell phones he returned the number and for others not. So to avoid problems, I ended up changing the application a little and making the user inform the number when I could not get it!

  • I also ended up doing the same thing, asking the user to enter the number, since - at least in the tests I was able to do - only Oi’s SIM cards had this information (in TIM and Vivo that I tested, the number was not stored, probably because operators take this information dynamically via telephone network).

  • Also, I decided not to try to check automatically before asking the user to type, because in addition to this functionality work only for a minority of users, it would be another plugin to keep in the app, besides requiring a permission that may seem intrusive to the user ("Allow the app to ... make and manage phone calls").

  • Yes. I would deny that permission. But the project I was doing was for a specific company and the users were the employees who already knew they would have to provide this information and its location to the app. In the end I was very satisfied with the result. The performance and practicality in developing with IONIC and Cordova surprised me a lot! :)

1


I do not know what version of the SDK you are using but from 23 have to ask for permission to read.

// Checar permissão function hasReadPermission() { window.plugins.sim.hasReadPermission(successCallback, errorCallback); }

// Pedir permissão function requestReadPermission() { // no callbacks required as this opens a popup which returns async window.plugins.sim.requestReadPermission(); }

  • 1

    I added the permissions. It worked on my genymotion emulator, but not on my phone. I’m testing for Ionic View, I don’t know if that’s it. To do a more accurate test, I will generate an apk and test again to see if it works.

  • 1

    In the end I could not get the number in a specific cell phone. I did some research and found that some mobile phones do not keep the number on the SIM card. For this reason it was not possible to rescue it. But I believe your answer should solve the problem for newer versions and as I was not using it yet, I marked it as correct! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.