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