简体   繁体   English

科尔多瓦PickContact不起作用

[英]Cordova pickcontact doesn't work

I try to pick a contact with cordova plugin contacts, but I still have a bug. 我尝试通过Cordova插件联系人来选择联系人,但是我仍然有一个错误。 My button #pickContact opens correctly the activity where I can tap on a contact. 我的按钮#pickContact可以正确打开我可以点击联系人的活动。 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). 当我返回页面时,出现错误消息OPERATION_CANCELLED_ERROR(代码6)。

I really don't understand where is the problem. 我真的不明白问题出在哪里。 I run my app on Android Marshmallow. 我在Android棉花糖上运行我的应用程序。 I thought about a permission problem, but my app can find correctly contacts with navigator.contacts.find, but not with navigator.contacts.pickContact 我想到了权限问题,但是我的应用程序可以通过navigator.contacts.find正确找到联系人,但是不能通过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. 根据联系人插件的参考文档,您选择的联系人将设置为JSON.stringify(contact)您可以alert它查看选择了哪些联系人(我已经使用了此插件,但是我不需要此功能来选择任何单个联系人,因此(不确定是否有完成按钮),然后按[完成]或[确定]按钮,这会将您重新导向到另一个功能,您可以在该功能上获得该联系人或满足您的下一个要求。

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. pickcontact打开您的本地应用程序“联系人”时,您的cordova应用程序将在后台删除。 On android, that means that you loose the state of your app, and so you have a bug. 在android上,这意味着您失去了应用程序的状态,因此有一个错误。 To solve the problem, you need to add onresume event on your js file, like this : 要解决此问题,您需要在js文件上添加onresume事件,如下所示:

 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. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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