I have some code to save contact info as a vcf. The code works fine, but when using on iOS the contact never saves. It only works on Mac. Not sure if there is something I am missing or what. I tried a couple libraries and got the same result.
Do not read. The warning said I do not have enough details and there is literally nothing else for me to add that is not a waste of time. Blah blah blah. Hello. Is this enough details?
Here is the code:
const generateVCFHandler = async (
name: string,
avatar: string,
links: Link[],
) => {
let vCardString = "BEGIN:VCARDnVERSION:3.0n";
// Name and formatted name
const names = name.split(" ");
const firstName = names[0];
const lastName = names.length > 1 ? names[names.length - 1] : "";
vCardString += `FN:${name}nN:${lastName};${firstName};;;n`;
// Add avatar
if (avatar) {
try {
const response = await fetch(
`/api/fetchImage?url=${encodeURIComponent(avatar)}`,
);
const { base64, mimeType } = await response.json();
if (base64 && mimeType) {
vCardString += `PHOTO;ENCODING=b;TYPE=${mimeType.split("/")[1]}:${base64}n`;
}
} catch (error) {
console.error("Failed to fetch image base64", error);
}
}
// Iterate over links to add relevant contact information
links.forEach((link) => {
switch (link.icon) {
case "CiPhone":
vCardString += `TEL;TYPE=CELL:${link.url.replace("tel:", "")}n`;
break;
case "CiMail":
vCardString += `EMAIL;TYPE=INTERNET:${link.url.replace("mailto:", "")}n`;
break;
case "CiFacebook":
vCardString += `URL;TYPE=Facebook:${link.url}n`;
break;
case "CiInstagram":
vCardString += `URL;TYPE=Instagram:${link.url}n`;
break;
case "CiLinkedin":
vCardString += `URL;TYPE=LinkedIn:${link.url}n`;
break;
case "BsTwitterX":
vCardString += `URL;TYPE=X:${link.url}n`;
break;
}
});
vCardString += "END:VCARD";
// Download the vCard
const blob = new Blob([vCardString], { type: "text/vcard;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "contact.vcf";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
Here is an example output:
BEGIN:VCARD
VERSION:3.0
FN:Bob User
N:User;Man;;;
TEL;TYPE=CELL:1 (123) 222-3333
URL;TYPE=Facebook:https://facebook.com/myprofile
URL;TYPE=Instagram:https://instagram.com/haha
END:VCARD