So speichern Sie vCards-js automatisch in iPhone- und Android-Kontakten
P粉722409996
P粉722409996 2024-03-26 22:47:43
0
1
386

Ich versuche, eine vCard auf dem Telefon des Benutzers (IOS und ANDROID) zu speichern. Ich verwende diesen 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();
});

Es funktioniert, aber dieser Code auf Android-Telefonen lädt zuerst die VCard herunter, dann muss der Benutzer zum Importieren auf „Herunterladen“ klicken. Ich möchte, dass meine Kontakte automatisch auf Android gespeichert werden, wenn der Benutzer diese Seite besucht, ohne dass Dateien heruntergeladen werden müssen. (Unter iOS stellt dies kein Problem dar, da Benutzer beim Besuch dieser Website automatisch zum Importieren von Kontakten weitergeleitet werden.)

注意: 我之前已经播过一个带有二维码的示例。当我扫描二维码时,他们将我重定向到导入联系人,我需要做的只是在手机上单击 save

Ich möchte dasselbe wie https://www.qr-code-generator.com, wenn ich auf die Registerkarte „vCard“ klicke. Aber wenn die Seite neu geladen wird, wird der QR-Code nicht gescannt

P粉722409996
P粉722409996

Antworte allen(1)
P粉116631591

您需要添加一个web share API的方法,根据您的代码,您可以添加navigator.share(),条件如if检查此API是否浏览器支持,则会自动下载其他下载链接。

像下面更新的代码一样:-

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();
  
  }
});
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!