简体   繁体   中英

Cordova pickcontact doesn't work

I try to pick a contact with cordova plugin contacts, but I still have a bug. My button #pickContact opens correctly the activity where I can tap on a contact. But when I tap on one, nothing happens. When I go back to my page, I have the error message OPERATION_CANCELLED_ERROR (code 6).

I really don't understand where is the problem. I run my app on Android Marshmallow. I thought about a permission problem, but my app can find correctly contacts with navigator.contacts.find, but not with navigator.contacts.pickContact

Here is my code :

 function pickContact() { navigator.contacts.pickContact(function(contact){ alert('ok !'); },function(err){ alert('bug !' + err); console.log('Error: ' + err); }); } var app = { // Application Constructor initialize: function() { this.onDeviceReady(); if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) { document.addEventListener("deviceready", this.onDeviceReady, false); } else { this.onDeviceReady(); } }, onDeviceReady: function() { $("#pickContact").click(pickContact); }, // Update DOM on a Received Event receivedEvent: function(id) { } }; app.initialize(); 
Thanks for your help !

As per the reference doc of contacts plugin your selected contact will set into JSON.stringify(contact) you can alert it to see which contacts are selected (I have used this plugin but I don't need this function to pick any single contact so not sure if there is any done button or not) then press done or ok button, which will redirect you to your another function where you can get that contacts or fulfill your next requirements.

function pickContact() {
    navigator.contacts.pickContact(function(contact){
        alert(JSON.stringify(contact));
        //This is added by me, on done button click or single selection
        setContacts(contact);
    },function(err){
        alert('bug !' + err);
        console.log('Error: ' + err);
    });
}
//This is added by me
function setContacts(ct)
{
    alert(JSON.stringify(ct));
    $("#contactlist").append(ct);

    //or

    var getData = JSON.parse(ct);
    if(getData.length > 1)
    {
        for(i=0;i<getData.length;i++)
        {
            $("#contactlist").append(getData[i]);
        }
    }
}

Let me know if I am wrong or right.

Thanks a lot for your answer. Unfortunately , your code doesn't works for me, but I found what to do :

When pickcontact opens your native app "contacts", your cordova app is removed on background. On android, that means that you loose the state of your app, and so you have a bug. To solve the problem, you need to add onresume event on your js file, like this :

 var app = { // Application Constructor initialize: function() { this.onDeviceReady(); if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) { document.addEventListener("deviceready", this.onDeviceReady, false); } else { this.onDeviceReady(); } }, onDeviceReady: function() { $("#pickContact").click(pickContact); }, onResume: function(resumeEvent) { //alert('onResume'); }, // Update DOM on a Received Event receivedEvent: function(id) { } }; app.initialize(); 

After that, you can retrieve your picked contact with a function like this :

 function pickContact() { navigator.contacts.pickContact(function(contact){ $("#divTest").append('<p>The following contact has been selected:' + JSON.stringify(contact)); },function(err){ alert('bug !' + err); console.log('Error: ' + err); }); } 

So, like everytime in programming, when you know the answer, that's easy. But when you don't know, you loose hours and hours...

I hope that will help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM