温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ionic项目中实现发短信和打电话

发布时间:2020-07-04 07:14:36 来源:网络 阅读:446 作者:smile_panda 栏目:开发技术

原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/ionic-sms-and-call/

最近做的一个ionic项目中需要实现发短信和打电话,总结了一下遇到的问题和实现的方法。

1.关于打电话

html中可以很方便的实现拨打电话先在config.xml中添加:

<access origin="tel:*" launch-external="yes"/>

然后在html中这样写:

<a href="tel:10086”>拨打电话10086</a>

但是我想获取点击打电话的时间,所以做了改动:

html中:

<button ng-click="callFriend($event, friend)"></button>

js中:

$scope.callFriend = function ($event, friend) {
   window.open('tel:' + friend.PhoneNumber);
   //获取打电话的时间
   var time=new Date();
}

有时不想要自动识别电话,为了防止电话识别,可以在头文件中添加这一句:

<meta name="format-detection" content="telephone=no">

2.关于发短信,是使用的ng-cordova的插件$cordovaSMS:

先添加该插件:

cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git

记得相应的要在app.js中依赖ng-cordova,在发送短信的控制器中依赖$cordovaSms

我实现的是点击发短信的按钮会弹出一个popup:

在html中添加点击事件:

<button ng-click="openSendMessage(phoneNumber)"></button>

在控制器中写发送短信的函数:

//打开发送短信的popup
$scope.openSendMessage = function (phonenumber) {
  $rootScope.sendMessagePopup.show(phonenumber)
    .then(function (result) {
      return result;
    });
};

$scope.sms = {
  number: ‘10086’,
  message: '生日快乐'
};
var options = {
  replaceLineBreaks: false, // true to replace \n by a new line, false by default
  android: {
    intent: '' // send SMS with the native android SMS messaging
    //intent: '' // send SMS without open any other app
    //intent: 'INTENT' // send SMS inside a default SMS app
  }
};
$scope.sendSMS = function () {
  $cordovaSms.send(‘10086’, '生日快乐', options)
    .then(function () {
      alert('发送短信成功');
    }, function (error) {
      alert('发送短信失败');
    });
};


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI