How to automatically save vCards-js to iPhone and Android contacts
P粉722409996
P粉722409996 2024-03-26 22:47:43
0
1
379

I'm trying to save a vCard on the user's phone (IOS and ANDROID). I'm using this code

window.addEventListener("load", function () {
  // Contact Information
  var contact = {
    ...
  };
  // create a vcard file
  var vcard = "BEGIN:VCARD\nVERSION:3.0\n"
    + "N:" + contact.name + ";;;\n"
    + "FN:" + contact.name + "\n"
    + "TEL;CELL:" + contact.phone + "\n"
    + "TEL;CELL:" + contact.mobile + "\n"
    + "EMAIL;HOME:" + contact.email + "\n"
    + "ADR;HOME:" + contact.address + "\n"
    + "ORG;WORK:" + contact.organization + "\n"
    + "TITLE:" + contact.title + "\n"
    + "URL:" + contact.url + "\n"
    + "NOTE:" + contact.notes + "\n"
    + "END:VCARD";  
  
  // var vcard = "BEGIN:VCARD\nVERSION:4.0\nFN:" + contact.name + "\nTEL;TYPE=work,voice:" + contact.phone + "\nEMAIL:" + contact.email + "\nEND:VCARD";
  var blob = new Blob([vcard], { type: "text/vcard" });
  var url = URL.createObjectURL(blob);
  
  const newLink = document.createElement('a');
  newLink.download = contact.name + ".vcf";
  newLink.textContent = contact.name;
  newLink.href = url;
  
  newLink.click();

  // window.close();
});

It works, but this code on Android phones first downloads the vcard, then the user needs to click download to import. What I want is that when the user comes to this page, my contacts are automatically saved on Android without downloading any files. (On iOS this is not a problem as when users visit this site they are automatically redirected to import contacts)

Note: I have broadcast an example with a QR code before. When I scan the QR code they redirect me to import contacts and all I need to do is click save

on my phone

I want the same thing as https://www.qr-code-generator.com when the vCard tab is clicked. But when the page reloads, the QR code is not scanned

P粉722409996
P粉722409996

reply all(1)
P粉116631591

You need to add a web share API method. According to your code, you can add navigator.share(), with conditions such as if to check whether this API is supported by the browser. Other download links will be automatically downloaded.

Like the updated code below: -

window.addEventListener("load", function () {
  // Contact Information
  var contact = {
    ...
  };
  // create a vcard file
  var vcard = "BEGIN:VCARD\nVERSION:3.0\n"
    + "N:" + contact.name + ";;;\n"
    + "FN:" + contact.name + "\n"
    + "TEL;CELL:" + contact.phone + "\n"
    + "TEL;CELL:" + contact.mobile + "\n"
    + "EMAIL;HOME:" + contact.email + "\n"
    + "ADR;HOME:" + contact.address + "\n"
    + "ORG;WORK:" + contact.organization + "\n"
    + "TITLE:" + contact.title + "\n"
    + "URL:" + contact.url + "\n"
    + "NOTE:" + contact.notes + "\n"
    + "END:VCARD";  
  
  // var vcard = "BEGIN:VCARD\nVERSION:4.0\nFN:" + contact.name + "\nTEL;TYPE=work,voice:" + contact.phone + "\nEMAIL:" + contact.email + "\nEND:VCARD";
  var blob = new Blob([vcard], { type: "text/vcard" });
  var url = URL.createObjectURL(blob);
  
  if (navigator.share) {
  
    navigator.share({
    
      title: 'New Contacts',
      text: 'Save contacts',
      files: [new File([blob], 'newcontact.vcf', { type: 'text/vcard' })],
    }).then(() => { });

  } else {
      const newLink = document.createElement('a');
      newLink.download = contact.name + ".vcf";
      newLink.textContent = contact.name;
      newLink.href = url;

      newLink.click();

      // window.close();
  
  }
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!