React Native vCards - vCards JS for use with React Native
Updated for newer versions of react-native-fs. Now listed as a peerDependency.
Create vCards to import contacts into Outlook, iOS, Mac OS, and Android devices from your website or application.
Install
Note that this package requires react-native-fs for saving files.
If you have installed rnpm, the easy way to install react-native-fs is the following:
npm install react-native-fs && rnpm link react-native-fs
Then to install this package, run the following:
npm install react-native-vcards@https://github.com/idxbroker/react-native-vcards.git --save
Usage
Below is a simple example of how to create a basic vCard and how to save it to a file, or view its contents from the console.
Basic vCard
import vCard from 'react-native-vcards';
contact = vCard();
contact.firstName = 'Eric';
contact.middleName = 'J';
contact.lastName = 'Nesser';
contact.organization = 'ACME Corporation';
contact.photo.attachFromUrl('https://avatars2.githubusercontent.com/u/5659221?v=3&s=460', 'JPEG');
contact.workPhone = '312-555-1212';
contact.birthday = new Date('01-01-1985');
contact.title = 'Software Developer';
contact.url = 'https://github.com/enesser';
contact.note = 'Notes on Eric';
contact.saveToFile('./eric-nesser.vcf');
console.log(contact.getFormattedString());
Embedding Images
You can embed images in the photo or logo field instead of linking to them from a URL using base64 encoding.
contact.photo.embedFromFile('/path/to/file.png');
contact.logo.embedFromFile('/path/to/file.png');
Complete Example
The following shows a vCard with everything filled out.
import vCard from 'react-native-vcards';
contact = vCard();
contact.firstName = 'Eric';
contact.middleName = 'J';
contact.lastName = 'Nesser';
contact.organization = 'ACME Corporation';
contact.photo.attachFromUrl('https://avatars2.githubusercontent.com/u/5659221?v=3&s=460', 'JPEG');
contact.photo.attachFromUrl('/path/to/file.jpeg');
contact.workPhone = '312-555-1212';
contact.birthday = new Date('01-01-1985');
contact.title = 'Software Developer';
contact.url = 'https://github.com/enesser';
contact.workUrl = 'https://acme-corporation/enesser';
contact.note = 'Notes on Eric';
contact.nickname = 'Scarface';
contact.namePrefix = 'Mr.';
contact.nameSuffix = 'JR';
contact.gender = 'M';
contact.anniversary = new Date('01-01-2004');
contact.role = 'Software Development';
contact.homePhone = '312-555-1313';
contact.cellPhone = '312-555-1414';
contact.pagerPhone = '312-555-1515';
contact.homeFax = '312-555-1616';
contact.workFax = '312-555-1717';
contact.email = 'e.nesser@emailhost.tld';
contact.workEmail = 'e.nesser@acme-corporation.tld';
contact.logo.attachFromUrl('https://avatars2.githubusercontent.com/u/5659221?v=3&s=460', 'JPEG');
contact.source = 'http://mywebpage/myvcard.vcf';
contact.homeAddress.label = 'Home Address';
contact.homeAddress.street = '123 Main Street';
contact.homeAddress.city = 'Chicago';
contact.homeAddress.stateProvince = 'IL';
contact.homeAddress.postalCode = '12345';
contact.homeAddress.countryRegion = 'United States of America';
contact.workAddress.label = 'Work Address';
contact.workAddress.street = '123 Corporate Loop\nSuite 500';
contact.workAddress.city = 'Los Angeles';
contact.workAddress.stateProvince = 'CA';
contact.workAddress.postalCode = '54321';
contact.workAddress.countryRegion = 'United States of America';
contact.socialUrls['facebook'] = 'https://...';
contact.socialUrls['linkedIn'] = 'https://...';
contact.socialUrls['twitter'] = 'https://...';
contact.socialUrls['flickr'] = 'https://...';
contact.socialUrls['custom'] = 'https://...';
contact.photo.embedFromFile('photo.jpg');
contact.logo.embedFromFile('logo.jpg');
contact.version = '3.0';
const documentPath = rnfs.DocumentDirectoryPath;
contact.saveToFile(`${documentPath}/eric-nesser.vcf`);
console.log(contact.getFormattedString());
Contributions
Contributions are always welcome!
Thanks to mplno, lop-cz, jkrenge.
License
Copyright (c) 2014-2015 Eric J Nesser MIT