0

我正在构建一个 iOS Phonegap 应用程序,该应用程序可以选择使用此 JavaScript 将信息添加到联系人条目:

function saveContact() {
  var contact = navigator.contacts.create();
  contact.displayName = "Name";

  var phoneNumber = [];
  phoneNumber[0] = new ContactField('main', '5555555555', true);
  contact.phoneNumbers = phoneNumber;

  var urlWeb = [];
  urlWeb[0] = new ContactField('home page', 'www.myurl.com', true);
  contact.urls = urlWeb;

  var streetAddress = [];
  streetAddress[0] = new ContactAddress('true', 'main', 'Full Address', '123 School St.', 'San Fransisco', 'CA', '00000', '');
  contact.addresses = streetAddress;

  var lastName = new ContactName();
  lastName.familyName = "Name";
  contact.name = lastName;

  var contactPhoto = [];
  contactPhoto[0] = new ContactField('photo', base64photo, false)
  contact.photos = contactPhoto;

  contact.note = contactNote;

  contact.save(contactSaveSuccess,contactSaveError);
}

当我执行此功能时,它运行良好并添加了联系人。当我第二次执行它时,它会创建第二个条目而不是更新它,正如我所理解的那样。我尝试使用以下方法添加 ID:

contact.id = 10000;

我用引号试了一下:

contact.id = '10000';

...但这没有用。我错过了什么?

4

2 回答 2

0

这是一个工作示例,我将 id 和我的 orgname 连接起来,但请随意使用您自己的字段,例如“firstname_lastname”.toLowerCase();

saveContact: function(event, idContact) {

    var name = $(".contactName"+idContact).text();
    var surname = $(".contactSurname"+idContact).text();
    var fonction = $(".contactFunction"+idContact).text();
    var email = $(".contactEmail"+idContact).text();
    var tel = $(".contactTel"+idContact).text();

    var contact = navigator.contacts.create();
    contact.id = idContact + "myorgname";
    contact.displayName = name;

    var contactName = new ContactName();
    contactName.givenName = surname;
    contactName.familyName = name;
    contact.name = contactName;

    var organizations = [];
    organizations[0] = new ContactOrganization(false, 'work', 'MyOrg','',fonction); // preferred
    contact.organizations = organizations;

    var emails = [];
    emails[0] = new ContactField('PhoneGap', email, true);
    contact.emails = emails;

    var phoneNumbers = [];
    phoneNumbers[0] = new ContactField('PhoneGap', tel, true);
    contact.phoneNumbers = phoneNumbers;

    contact.save(this.onContactSaved, this.onContactSavedError);
},
于 2014-05-14T18:47:07.350 回答
0

在保存之前尝试在联系人对象上设置 rawId。
contact.rawId = contact.id 也是如此;
然后尝试保存

于 2013-05-27T08:31:27.960 回答