Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with cordova/phonegap contacts.
This is the code i am trying to execute, i put in an external javascript file:

function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
options.filter = "*";
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
}

// onSuccess: Get a snapshot of the current contacts

function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
    console.log("Display Name = " + contacts[i].displayName);
}
}

// onError: Failed to get the contacts

function onError(contactError) {
alert('onError!');
}

This is the code from the phonegap API documentation: link

The original code is to find all contacts with 'bob' in every field.
I changed it to "*"(Just a star) for all my contacts.

The function onDeviceReady is just called by a button click.

The error I get in the logcat is this:

[INFO:CONSOLE(81)] "Uncaught ReferenceError: ContactFindOptions is not defined"  
81 is the linenumber with: var options = new ContactFindOptions();

Does anyone know what to do to get the function ContactFindOptions() work?

If you need more info, just let me know.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
317 views
Welcome To Ask or Share your Answers For Others

1 Answer

This may be happening because your cordova is not understanding this function. You need to add the plugin, you need to add the contacts plugin. In your cordova project run this using command line:

cordova plugin add org.apache.cordova.contacts

This will add following to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

and add following to resxmlconfig.xml

<feature name="Contacts">
   <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...