San*_*eph 4 html javascript android cordova
我正在使用手机间隙开发应用程序.我正试图访问移动设备上的联系人,因为我稍后会使用它们.我现在正在尝试编写代码以在移动设备上查找联系人.这是我正在使用的JS文件:
alert('Starting JS');
var TAP = ('ontouchend' in window) ? 'touchend' : 'click';
alert('I entered the function');
document.addEventListener('DOMContentLoaded', function () {
alert('I entered the second function');
x$('#friendSubmit').on(TAP, function () {
var filter = x$('#friendName')[0].value;
alert('I entered the third function');
if (!filter)
{
alert('Cant find contacts');
// no contents
return;
}
else
{
findContactByName(filter, function (contacts)
{
alert(contacts.length + ' contact(s) found matching "' +filter + '"');
}
); }
}); });
function findContactByName(name, callback) {
function onError() {
alert('Error: unable to read contacts');
};
var fields = ["displayName", "name"],
options = new ContactFindOptions();
options.filter = name;
options.multiple = true;
// find contacts
navigator.service.contacts.find(fields, callback, onError,
options);
?}
Run Code Online (Sandbox Code Playgroud)
没有任何警报被警告,因此似乎代码中出现了问题(但是当我删除"findContactByName"函数时它会发出警报.
你知道吗?如果我应该添加任何类型的插件,或更新任何东西,以便这些功能可以工作?我正在使用cordova版本1.6.1,我更新了清单上的权限,以便能够访问联系人.那么,你知道我的代码有什么问题吗?为什么它不起作用?
非常感谢.
您在等待deviceready事件(加载PhoneGap)吗?
以下代码适用于将带有名称字段的所有联系人放入名称数组中:
function onDeviceReady() {
// specify contact search criteria
var options = new ContactFindOptions();
options.filter=""; // empty search string returns all contacts
options.multiple=true; // return multiple results
filter = ["displayName"]; // return contact.displayName field
// find contacts
navigator.contacts.find(filter, onSuccess, onError, options);
}
var names = [];
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
for (var i=0; i<contacts.length; i++) {
if (contacts[i].displayName) { // many contacts don't have displayName
names.push(contacts[i].displayName);
}
}
alert('contacts loaded');
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10161 次 |
最近记录: |