Tweeting with React Native and Linking.openURL

If you’re targeting Asia, or even Western countries sometimes tweets can be a pretty invaluable source of free advertising for your mobile app. Lately I wanted to add tweeting functionality to a new React Native project I’m working on and a Google search didn’t yield such great results so I thought I’d document my solution here. There are a few good sharing libraries for React Native, but some haven’t been updated for awhile(which is understandable, most open source work isn’t paid). I really like react-native-share and it came close with it’s shareSingle method but tweeting doesn’t seem to work properly on the latest version of iOS.

After reading through a couple of issues on different React Native projects I realized that Twitter actually has a guide for web intents. After reading this I decided it might actually be easier to just generate an intent link and then open that with React Native’s Linking API(Linking.openURL). In the end I was pretty satisfied with the results. The best thing about the link is, if the user doesn’t have the Twitter app installed it will just revert to the browser — more on this later.

Tweeting with the Linking(Linking.openURL) API

It’s pretty easy and quick to get this all working. I have a Helper class with some static methods in it and I added this to it:

This method takes the text, URL and hashtags for your tweet and creates a link. Hashtags need to be in String form, separated by commas(ex: ‘tag1,tag2,tag3’). Once you create the link it’s a simple as opening it with the Linking API’s openURL method:

The Result

And now for the results of this method. Everything is quite seamless on both Android and iOS. Here are some iOS screenshots anyway, one is of the simulator(doesn’t have Twitter installed) and the other is of my SE(does have Twitter installed). Forgive the Japanese, you get the idea.

Tweet on iOS simulator
iOS simulator and Safari
Tweet on physical iOS device
iPhone SE with Twitter installed

Wrap Up

That’s pretty much it for today. I’m going to try and write shorter technical posts more often, some of my most read posts on Medium are short-form tutorials/tips. If you’re interested in learning how to make a simple counter in React Native check out my last post. If you have any questions feel free to comment below or clap.