功能描述:点击电话号码,可选择拨打电话或发短信的功能。
实现方法:
1. 打电话H5的实现方法特别简单。
a) 只需要在ionic项目的config.xml文件中添加如下代码
<accessorigin="tel:*"launch-external="yes"/>
b) 代码实现也特别简单:
i> html中直接写就可以了
<a href="17310133443">打电话</a>
i> 在controller.js里的实现方法
$scope.callPhone = function (phonenumber){ $window.location.href = "tel:" + phonenumber; };
2. 发短信(使用ng-cordova的插件$cordovaSMS)
a) 首先执行命令
cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git
安装不上可执行
ionic plugin add cordova-sms-plugin
b) app.js中注入ngCordova
controller.js中注入$cordovaSms
c) 然后调用下面方法就可以了
$scope.sendMsg = function (mobilePhone) { console.log("发送消息:" + mobilePhone); var options = { replaceLineBreaks: false,//替换换行符,true用行代替空格,false是默认样式 android: { intent: 'INTENT' //用安卓自带的短信发送短信
//intent: '' //用其他软件发送短信 } }; $cordovaSms.send(mobilePhone, '内容', options) };
3. 整合
a) 利用ionic的上拉菜单$ionicActionSheet实现代码
$scope.callPhone = function(mobilePhone){ var hidesheet = $ionicActionSheet.show({ titleText: "操作电话", buttons: [ { text: "呼叫" }, { text: "发短信" } ], buttonClicked: function(index) { if(index == 0){ $window.location.href = "tel:" + mobilePhone; } if(index == 1){ var options = { replaceLineBreaks: false, android: { intent: 'INTENT' //intent: '' } }; $cordovaSms.send(mobilePhone, '', options); } hidesheet(); } }); };