【问题标题】:Unable to list SMS using cordova plugin in Ionic无法在 Ionic 中使用 cordova 插件列出 SMS
【发布时间】:2018-12-06 04:58:36
【问题描述】:

我已尝试使用以下链接中提供的插件

https://github.com/floatinghotpot/cordova-plugin-sms?files=1

我收到一个错误短信未定义

下面是我的控制器代码

angular.module('starter.controllers', [])
  .controller('SMSCtrl', function($scope ,$ionicPopup ) {
    $scope.data = {};
    $scope.SMSlist = function() {
      console.log("Button click");
      var filter = {
        box : 'inbox', //inbox, sent, draft
        //following 4 filters should not be applied together, they are OR relationship
        read :1, //0 for unread and 1 for already
        // _id: 1234, //specify the msg id
        // body : 'This is a test SMS' , //content to match
        // address: '+9731151243';

        indexFrom : 0,
        maxCount : 10,

      };   
      if(SMS) SMS.listSMS(filter,function(data){
        if(Array.isArray(data)){
          var sms = data[i];
          console.log(sms);
          $scope.article = sms;
      }
    }, function(err) {
      var alertPopup = $ionicPopup.alert({
        title: 'Reading Failed!',
        template: 'Read Failed'
      });
    })
  }
})

任何帮助将不胜感激

【问题讨论】:

  • 您确定 deviceReady 在您尝试执行该代码之前已触发吗?您可以通过在两个地方使用 alert(); 最轻松地检查这一点:在 deviceReady 处理程序上和在 if (SMS) 之前。这样你就可以看到哪个先完成。
  • 似乎是我没有初始化第三方插件,但不知道如何初始化。我在 app.js 中的 run function .run(function($ionicPlatform) { $ionicPlatform.ready(function() { var phoneNumber = window.plugins.sms; sms.get(function(result) { //结果 }, function() { //error }); }); });
  • 所以 deviceReadyif (SMS) 之前触发?在这种情况下,您是使用Cordova CLI 还是PhoneGap Build 来构建您的应用程序?
  • 我正在使用 Cordova CLI
  • 我假设你已经完成了cordova plugin add com.rjfun.cordova.sms,对吧?其他插件是否正常工作?另外,你是用模拟器还是真机来测试呢?以及哪个平台(iOS、Android、WP 等)?

标签: angularjs cordova ionic-framework cordova-plugins


【解决方案1】:

我遇到了同样的问题。

您需要使用短信插件中的

$ionicPlatform.ready(function() {
   // Your plugin use here
}

【讨论】:

    【解决方案2】:

    试试看

    if(window.SMS) window.SMS.listSMS(filter,function(data){
        if(Array.isArray(data)){
          var sms = data[i];
          console.log(sms);
          $scope.article = sms;
      }
    

    【讨论】:

      【解决方案3】:

      要解决“短信未定义”,需要在@Component({ ... })之前添加declare var SMS: any;

      然后,要列出 SMS,您必须从 JSON 中提取所有值(您的 data 在 JSON 中)。 为此,您只需要在data 之前添加JSON.stringify(data); 并删除function 即可:

      if(SMS) SMS.listSMS(filter, (data)=>{
      JSON.stringify(data);
       if(Array.isArray(data)) {
          for(var data in data) {
              this.msg = ListSms[data].address;
          }
      }
      },Error=>{}),
      

      所以,this.msg 现在拥有所有的短信。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 2018-05-10
        • 2015-12-11
        • 1970-01-01
        • 2017-05-23
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多