在html5下利用Native.js來獲取系統通信錄管理對象AddressBook,再調用對象中的find()方法查找出系統通訊錄中的全部聯繫人。
API:http://www.html5plus.org/doc/zh_cn/contacts.html#plus.contacts.AddressBook
代碼:javascript
<!doctype html> <html> <head> <meta charset="UTF-8"> <title></title> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <link href="../../css/mui.min.css" rel="stylesheet" /> <style> </style> </head> <body> <header class="mui-bar mui-bar-nav"> <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a> <h1 class="mui-title">聯繫人</h1> </header> <div class="mui-content"> <ul class="mui-table-view" id="listArea"> <li class="mui-table-view-cell" id="mode"> # </li> </ul> </div> <script src="../../js/mui.min.js"></script> <script type="text/javascript"> mui.init(); mui.plusReady(function() { // 擴展API加載完畢,如今能夠正常調用擴展API plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) { addressbook.find(["displayName", "phoneNumbers"], function(contacts) { for(var i = 0, len = contacts.length; i < len; i++) { var li = document.getElementById("mode").cloneNode(true); li.innerHTML = contacts[i].displayName + "<br/>手機:" + (contacts[i].phoneNumbers.length == 0 ? "" : contacts[i].phoneNumbers[0].value); document.getElementById("listArea").appendChild(li); } }, function() { mui.toast("獲取電話簿失敗 "); }, { multiple: true }); }, function(e) { mui.toast("獲取電話簿失敗 " + e.message); }); }); </script> </body> </html>