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

来源:互联网 发布:淘宝垃圾短信轰炸机 编辑:程序博客网 时间:2024/04/26 13:53

原文地址: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('发送短信失败');    });};
0 0