自动保存vCards-js到iPhone和Android联系人的方法
P粉722409996
P粉722409996 2024-03-26 22:47:43
0
1
387

我正在尝试在用户手机(IOS 和 ANDROID)上保存电子名片。我正在使用这段代码

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();
});

它可以工作,但是 Android 手机上的此代码首先下载 vcard,然后用户需要单击下载才能导入。我想要的是,当用户进入此页面时,我的联系人会自动保存在 Android 上,而无需下载任何文件。 (在 IOS 上这不是问题,因为当用户访问此网站时,他们会自动重定向以导入联系人)

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

当单击 vCard 选项卡时,我想要与 https://www.qr-code-generator.com 相同的东西。但是当页面重新加载时,不扫描二维码

P粉722409996
P粉722409996

全部回复(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();
  
  }
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!