Phonegap getting sim information using cordova-plu

2019-08-01 15:39发布

I'm new to mobile development. I'm using PhoneGap and I want to add a feature in my app to get the phone number from the SIM card, both on iOS and Android. I'm using this plugin:

cordova-plugin-sim

I'm using this very simple index code to display whatever information I can retrieve from the plugin.

<!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>SIM</title>
            <script type="text/javascript" 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>
        </head>
        <body>
            <p id="simInfo"></p>
        </body>
    </html>

This does not display anything. Is there anything I'm doing wrong or missing?

3条回答
何必那么认真
2楼-- · 2019-08-01 15:54

I found this ::

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

I did some changes in code but I got "undefined" message.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>SIM</title>
        <script type="text/javascript" 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);
                document.getElementById("phone").innerHTML=JSON.stringify(result.phoneNumber);
                document.getElementById("carrierName").innerHTML=JSON.stringify(result.carrierName);
            }
            function errorCallback(error) {
                document.getElementById("simInfo").innerHTML=JSON.stringify(error);
            }
        </script>
    </head>
    <body>
        SimInfo :: <p id="simInfo"></p>
        <br />
        Phone Number :: <p id="phone"></p>
        <br />
        Carrier Name :: <p id="carrierName"></p>
        <br />
    </body>
</html>

I attached the result image please check this.

enter image description here

please check this. Thanks.

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-01 16:08

Are you testing on iOS or Android? On Android 6.0 and above you need to implement.

  window.plugins.sim.requestReadPermission(successCallback, errorCallback);

Also looking at your code. In the errorCallback function, result will be undefined.

You need to change this.

    function errorCallback(error) {
        document.getElementById("simInfo").innerHTML=JSON.stringify(result);
    }

To this

    function errorCallback(error) {
        document.getElementById("simInfo").innerHTML=JSON.stringify(error);
    }
查看更多
男人必须洒脱
4楼-- · 2019-08-01 16:15

It gets numbers saved by user as phone number of the sim. If nothing is saved, it will be empty string. I am using redmi note 3. It is there in settings->sim cards & mobile networks->sim1/2->Edit sim card number

查看更多
登录 后发表回答