Saya cuba menyimpan vCard pada telefon pengguna (IOS dan ANDROID). Saya menggunakan kod ini
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(); });
Ia berfungsi, tetapi kod ini pada telefon Android mula-mula memuat turun vkad, kemudian pengguna perlu mengklik muat turun untuk mengimport. Apa yang saya mahu ialah apabila pengguna datang ke halaman ini, kenalan saya disimpan secara automatik pada Android tanpa memuat turun sebarang fail. (Pada IOS ini tidak menjadi masalah kerana apabila pengguna melawat tapak web ini, mereka dialihkan secara automatik untuk mengimport kenalan)
注意:
我之前已經(jīng)播過一個(gè)帶有二維碼的示例。當(dāng)我掃描二維碼時(shí),他們將我重定向到導(dǎo)入聯(lián)系人,我需要做的只是在手機(jī)上單擊 save
Saya mahu perkara yang sama seperti https://www.qr-code-generator.com apabila mengklik tab vCard. Tetapi apabila halaman dimuat semula, kod QR tidak diimbas
Anda perlu menambah kaedah web share Mengikut kod anda, anda boleh menambah navigator.share() Jika syaratnya seperti menyemak sama ada API ini disokong oleh penyemak imbas, pautan muat turun lain akan menjadi secara automatik dimuat turun.
Suka kod yang dikemas kini di bawah:-
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(); } });