Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
react-native-social-share
Advanced tools
Use the iOS native Twitter and Facebook share view from react native
Use the build in share view from iOS to let the user share on Facebook and Twitter. It will use the users existing account without having to get new authorizations. You can even preset text, image and link for the share view.
In other words a React Native wrapper for the SLComposeViewController
npm install react-native-social-share --save
Libraries
➜ Add Files to [your project's name]
node_modules
➜ react-native-social-share
➜ iOS and add KDSocialShare.h
and KDSocialShare.m
Build Phases
➜ Link Binary With Libraries
phaseSocial.framework
to ➜ Link Binary With Libraries
build phase of your project (click the '+' and search for 'social').Cmd+R
)Now you can implement the share popups in your react native code.
First you should make the native implementation available in the react code by inserting the following line in the top of the file
var KDSocialShare = require('NativeModules').KDSocialShare;
After doing that you will be able to popup the share views from your own functions. I made two examples below one for Facebook and one for Twitter
tweet : function() {
KDSocialShare.tweet({
'text':'Global democratized marketplace for art',
'link':'https://artboost.com/',
'imagelink':'https://artboost.com/apple-touch-icon-144x144.png',
},
(results) => {
console.log(results);
}
);
},
shareOnFacebook : function() {
KDSocialShare.shareOnFacebook({
'text':'Global democratized marketplace for art',
'link':'https://artboost.com/',
'imagelink':'https://artboost.com/apple-touch-icon-144x144.png',
},
(results) => {
console.log(results);
}
);
},
The two implementations take the following paramters
KDSocialShare.shareOnFacebook(options [object], callback [function])
KDSocialShare.tweet(options [object], callback [function])
The options object lets you prepopulate the share view for the user. You can use the following parameters:
Parameter | Desciption |
---|---|
text | Sets the initial text of the message on the SLComposeViewController instance. |
imagelink | Adds an image file from the given publicly available URL as attachments to the message. |
text | Adds a URL to the message. The method automatically handles the URL shortening. |
The callback function runs when the native environment has information for the react environment
Callback | Desciption |
---|---|
"success" | Native call made by the viewController - SLComposeViewControllerResultDone – The user sent the composed message by touching the Send button. |
"cancelled" | Native call made by the viewController - SLComposeViewControllerResultCancelled – The user cancelled the composition session by touching the Cancel button. |
"not_available" | The selected service eg. Facebook, is not available. This can be because the user has not signed in to Facebook on the device or maybe there is no internet access. |
You can use these callbacks to present alerts to the user. For example tell the user to login to a certain service.
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
} = React;
var KDSocialShare = require('NativeModules').KDSocialShare;
var ReactNativeSocialShare = React.createClass({
tweet : function() {
KDSocialShare.tweet({
'text':'Global democratized marketplace for art',
'link':'https://artboost.com/',
'imagelink':'https://artboost.com/apple-touch-icon-144x144.png',
},
(results) => {
console.log(results);
}
);
},
shareOnFacebook : function() {
KDSocialShare.shareOnFacebook({
'text':'Global democratized marketplace for art',
'link':'https://artboost.com/',
'imagelink':'https://artboost.com/apple-touch-icon-144x144.png',
},
(results) => {
console.log(results);
}
);
},
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Twitter and Facebook share
</Text>
<Text style={styles.instructions}>
Try tapping one of the buttons
</Text>
<View style={styles.seperator}/>
<TouchableHighlight onPress={this.tweet}>
<View style={{alignItems: 'center',justifyContent:'center', width: 150, height: 50,backgroundColor:'#00aced'}}>
<Text style={{color:'#ffffff',fontWeight:800,}}>Share on Twitter</Text>
</View>
</TouchableHighlight>
<View style={styles.seperator}/>
<TouchableHighlight onPress={this.shareOnFacebook}>
<View style={{alignItems: 'center',justifyContent:'center', width: 150, height: 50,backgroundColor:'#3b5998'}}>
<Text style={{color:'#ffffff',fontWeight:800,}}>Share on Facebook</Text>
</View>
</TouchableHighlight>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
seperator:{
marginBottom: 20
}
});
AppRegistry.registerComponent('ReactNativeSocialShare', () => ReactNativeSocialShare);
FAQs
Use the iOS native Twitter and Facebook share view from react native
The npm package react-native-social-share receives a total of 126 weekly downloads. As such, react-native-social-share popularity was classified as not popular.
We found that react-native-social-share demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.