亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

自動保存vCards-js到iPhone和Android聯(lián)系人的方法
P粉722409996
P粉722409996 2024-03-26 22:47:43
0
1
778

我正在嘗試在用戶手機(jī)(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 手機(jī)上的此代碼首先下載 vcard,然后用戶需要單擊下載才能導(dǎo)入。我想要的是,當(dāng)用戶進(jìn)入此頁面時,我的聯(lián)系人會自動保存在 Android 上,而無需下載任何文件。 (在 IOS 上這不是問題,因為當(dāng)用戶訪問此網(wǎng)站時,他們會自動重定向以導(dǎo)入聯(lián)系人)

注意: 我之前已經(jīng)播過一個帶有二維碼的示例。當(dāng)我掃描二維碼時,他們將我重定向到導(dǎo)入聯(lián)系人,我需要做的只是在手機(jī)上單擊 save

當(dāng)單擊 vCard 選項卡時,我想要與 https://www.qr-code-generator.com 相同的東西。但是當(dāng)頁面重新加載時,不掃描二維碼

P粉722409996
P粉722409996

全部回復(fù)(1)
P粉116631591

您需要添加一個web share API的方法,根據(jù)您的代碼,您可以添加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();
  
  }
});
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板