Create a vCard/contact without saving it to device

Is there a way to create a vCard or contact with ‘expo-contacts’ without saving it or editing it on the users device? For my purposes I don’t want to edit the users contacts but I would like them to be able to send a contact via sms. For example - user clicks send contact from my expo app, then the users texting/sms app opens on their device and the body is prefilled with a vCard or contact provided by my app - at which point the user can type in a phone or select from their contacts who they want to send the message to. I basically want to use the functionality of Contacts.addContactAsync(contact, containerId), but instead of saving it to the users device i want to sms the contact. Any help would be greatly appreciated.

I guess another solution would be a way to convert JSON user object into a vcf?

Hi

You might want to look at using a JavaScript library for creating a VCF-formatted string.

e.g. see the following snack:

The vCard-related code was taken directly from the example on vcards-js’ GitHub repository.
vcf is another one that looks promising.

1 Like

Thanks @wodin! I did run across vcard js before posting but wasn’t happy with it given some security vulnerabilities and the fact it hasn’t bee updated in a over a year. I got that sorted out though and I am able to produce a vcard with it.The issue now is sharing a file with expo. Expo-sharing is still unavailable with managed workflows and expo sms does not support MMS or files being the body. I tried using the formatted vcard strings that vcf and vcard js offer but they result in sending the user the literal text of the vcard file and not the file itself. Any ideas on how to share a file or send MMS with an expo managed workflow?

I appreciate your help and sorry for the super slow response i have bee busy lately!

I’ve not tried sharing stuff from Expo myself, but I don’t see anything in the sharing docs to imply that you would need to eject? So it looks to me like it should work in a managed app. As the installation instructions say, if you’re installing it in a bare app you need to do some extra stuff, but in theory it should work fine in a managed app.

OK, I’ve just given it a try now and more or less got something working. It seems you have to save it to a file. You can’t e.g. share a “data:...” URL. It has to be a “file:///...” URL.

When I tested it on my Android phone I seemed to get best results with a Version 2.1 VCard. It did not offer me the SMS app as a sharing target, but it did offer me e.g. WhatsApp and Gmail. If I choose Gmail it opens an e-mail with the .vcf file as an attachment. I’m not sure if things would work better with a different mime type, file extension or VCard version.

Maybe this snack will help:

2 Likes

I found those docs like 2 minutes after i posted haha. I was looking at expo-sharing on NPM and it still has an empty link and says “If you follow the link and there is no documentation available then this library is not yet usable within managed projects” so thats what threw me off.

I was able to replicate your snack last evening as well after stumbling upon the expo-file-system. Same issue here on IOS - SMS app is not a sharing target but i am able to email it. I tried setting an options object {UTI:‘public.vcard’} to see if it would allow me to target sms but it was to no avail.

On a side note I tested sending a vcard through MMS with the Twilio API and it works no problem. I really feel l’m missing something and don’t see how RN or EXPO would be unable to send MMS.

Thanks again!!

Additionally I tested it with a JPG and sms is still not a sharing target so i don’t think the problem is the vcf/vcard.

Using the File System like @wodin suggested above and swapping out Sharing with Share works perfect. I set the URL value on Share.share() to the local URI returned from the FS downloadAsync. The only problem here is URI is IOS only so I don’t have an android solution :man_shrugging: I guess expo-sharing for some reason or another cannot access SMS.

1 Like

If you have a solution for iOS and need something for Android, maybe IntentLauncher will do it. Poking around on Stack overflow I came across this answer:

I noticed that in that answer they are using a mime type of text/x-vcard. So I tried using expo-sharing with that mime type and I was then able to share to the SMS app :slight_smile:

It does seem to attach the vcard to the message. Haven’t tried actually sending it.

So basically:

          const options = {
            mimeType: 'text/x-vcard',
            dialogTitle: 'Share vcard',
          };
[...]
          Sharing.shareAsync(fileUri, options)
            .then((data) => {
[...]

If the above doesn’t work for you, maybe try IntentLauncher.

2 Likes

Awesome thanks for your help! I can confirm setting mimeType doesn’t work for IOS but it does for android. Ill have to check what platform they are on before hand but thats simple enough. I think this is the only solution for the time being. Thanks!!

1 Like

Hey, I hadn’t seen this before, but someone just mentioned it in another forum post and I thought it might be relevant to you. I haven’t tried it myself:

https://docs.expo.io/versions/v38.0.0/sdk/sms/

1 Like

@wodin thanks! As best i can tell the only data you can pass in is a string so a file or vcard would not work.

Well now that i look it over again i see an option for attachments now. :man_shrugging: I’m going to give it a try and see because this would definitely be a better solution.

1 Like