How to Write a vCard
Introduction to Writing vCards
A vCard (Virtual Contact File) allows you to share contact information seamlessly. When a user taps an NFC tag with a vCard, their phone will prompt them to save the contact directly to their address book. This is the core of a digital business card.
The vCard data is a specially formatted string. You can use our Free vCard Generator to create this data easily.
Code Example
To write a vCard, you need to create an NDEF message with a record of type 'mime' and a mediaType of 'text/vcard'. The data must be a DataView of the vCard text content.
async function writeVCard(vCardString) {
if (!('NDEFReader' in window)) {
console.log('Web NFC is not supported by this browser.');
return;
}
try {
const writer = new NDEFReader();
const encoder = new TextEncoder();
const vCardData = encoder.encode(vCardString);
await writer.write({
records: [
{
recordType: 'mime',
mediaType: 'text/vcard',
data: vCardData,
},
],
});
console.log('vCard record written successfully!');
} catch (error) {
console.error('Error writing vCard record:', error);
}
}
// Example usage (vCard data is simplified):
// const vcard = 'BEGIN:VCARD\nVERSION:3.0\nFN:John Doe\nTEL;TYPE=work,voice:1234567890\nEMAIL:john.doe@example.com\nEND:VCARD';
// writeVCard(vcard);Use Cases
- Creating digital business cards.
- Sharing contact information at events and conferences.
- Adding contact details to equipment or assets for easy support access.