cordova插件- Contacts

小编:管理员 840阅读 2022.09.13

  • 添加插件

$cordova plugin add cordova-plugin-contacts

图 8出现如上则添加成功

  • 插件的使用
  • Methods
  • navigator.contacts.create
  • navigator.contacts.find
  • navigator.contacts.pickContact
  • Example

function openContacts () {

    navigator.contacts.pickContact(function (contact) {

        console.log('the follow contact has been selected' +JSON.stringify(contact));

    },function (err) {
        console.log('error' +err);
    })

}
复制

选择之后返回的是一串json数据

{
复制
"id": 3,
复制
"rawId": null,
复制
"displayName": null,
复制
"name": {
复制
"givenName": "John",
复制
"honorificSuffix": null,
复制
"formatted": "John Appleseed",
复制
"middleName": null,
复制
"familyName": "Appleseed",
复制
"honorificPrefix": null
复制
},
复制
"nickname": null,
复制
"phoneNumbers": [
复制
{
复制
"value": "888-555-5512",
复制
"pref": false,
复制
"id": 0,
复制
"type": "mobile"
复制
},
复制
{
复制
"value": "888-555-1212",
复制
"pref": false,
复制
"id": 1,
复制
"type": "home"
复制
}
复制
],
复制
"emails": [
复制
{
复制
"value": "[email protected]",
复制
"pref": false,
复制
"id": 0,
复制
"type": "work"
复制
}
复制
],
复制
"addresses": [
复制
{
复制
"pref": "false",
复制
"locality": "Atlanta",
复制
"region": "GA",
复制
"id": 0,
复制
"postalCode": "30303",
复制
"country": "USA",
复制
"type": "work",
复制
"streetAddress": "3494 Kuhl Avenue"
复制
},
复制
{
复制
"pref": "false",
复制
"locality": "Atlanta",
复制
"region": "GA",
复制
"id": 1,
复制
"postalCode": "30303",
复制
"country": "USA",
复制
"type": "home",
复制
"streetAddress": "1234 Laurel Street"
复制
}
复制
],
复制
"ims": null,
复制
"organizations": null,
复制
"birthday": "1980-06-22T12:00:00.000Z",
复制
"note": "College roommate",
复制
"photos": null,
复制
"categories": null,
复制
"urls": null
复制
}
复制

具体字段的含义参见:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html



function createContact () {

    var myContact = navigator.contacts.create({"displayName":"test user"});

    myContact.save(function (contact) {

        console.log('the contact has created' +json.stringify(contact));

    },function (err) {

        console.log('error' +err);
    });

}
复制


function findContct () {

    function onSuccess(contact) {

        alert(' 找到=' +contact.length +'个联系人');
    }

    function onError(error) {

        alert('error'+error);

    }
    var  options = new ContactFindOptions();
    options.filter = "test user";
    options.multiple = true;
    options.desiredFields = [navigator.contacts.fieldType.id];
    options.hasPhoneNumber = false;

    var  fields = [navigator.contacts.fieldType.displayName,navigator.contacts.fieldType.name];
    navigator.contacts.find(fields,onSuccess,onError,options);

}
复制
关联标签: