react native 中函数Share示例与说明
2017-10-14 17:23
281 查看
/**
* 函数Share(用于在Android设备上打开一个对话框来分享或发送文本内容)
* */
import React,{PureComponent} from 'react'
import {View,Text,TouchableOpacity, Share} from 'react-native'
class ShareFunction extends PureComponent {
constructor(props) {
super(props);
this.state = {
result: ''
};
}
render() {
return (
<View>
<TouchableOpacity onPress={()=>Share.share({
message: '这个是百度的网址'
},{
dialogTitle: '分享和发送'})}
style={{height:50,backgroundColor:'#0f0',borderRadius:30,marginTop:30,justifyContent:'center',alignItems:'center'}}
>
<View>
<Text>简单</Text>
<Text style={{textAlign:'center'}}>分享和发送(Share)</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={this._shareMessage}
style={{height:50,backgroundColor:'#0f0',borderRadius:30,marginTop:30,justifyContent:'center',alignItems:'center'}}
>
<View>
<Text style={{textAlign:'center'}}>分享和发送URL(Share)</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={this._shareText}
style={{height:50,backgroundColor:'#0f0',borderRadius:30,marginTop:30,justifyContent:'center',alignItems:'center'}}
>
<View>
<Text style={{textAlign:'center'}}>分享和发送(Share)</Text>
</View>
</TouchableOpacity>
<Text style={{marginTop:50}}>{this.state.result}</Text>
</View>
);
}
_shareMessage=()=> {
Share.share({
message: '这个是百度的网址t'
})
.then(this._showResult)
.catch((error) => this.setState({result: '错误提示: ' + error.message}));
};
_shareText=()=> {
Share.share({
message: '这个是百度的网址',
url: 'https://www.baidu.com',
title: '百度'
}, {
dialogTitle: '分享百度链接到',
})
.then(this._showResult)
.catch((error) => this.setState({result: '错误提示: ' + error.message}));
};
//判断是否分享成功
_showResult=(result)=> {
if (result.action === Share.sharedAction) {
if (result.activityType) {
this.setState({result: 'shared with an activityType: ' + result.activityType});
} else {
this.setState({result: '分享成功'});
}
} else if (result.action === Share.dismissedAction) {
this.setState({result: '分享拒绝'});
}
}
}
export default ShareFunction;
/***
static share(content, options)
打开一个对话框来共享文本内容。
在Android中,返回一个Promise,它始终使用Share.sharedAction操作来解决。
Content
message - 要分享的消息至少需要一个
title - 消息的标题
Options
dialogTitle
//对话框标题,默认为选择操作
static sharedAction()
内容已成功共享。.
static dismissedAction()
该对话框已被拒绝. @platform ios
* ***/
相关文章推荐
- ReactNative页面跳转Navigator实现的示例代码
- React Native中TabBarIOS的简单使用方法示例
- react-native 完整实现登录功能的示例代码
- react-native-fs实现文件下载、文本存储的示例代码
- ReactNative实现图片上传功能的示例代码
- RN(react native)入坑指南-02,一个登录示例
- Jquery DataTable 函数说明及示例
- fopen()、fwrite()、fread()函数使用说明与示例
- fopen()、fwrite()、fread()函数使用说明与示例
- Transact SQL 常用语句以及函数简易说明(含示例)
- React Native使用Modal自定义分享界面的示例代码
- react native 文字轮播的实现示例
- fopen()、fwrite()、fread()函数使用说明与示例
- react-native使用react-navigation进行页面跳转导航的示例
- react-native-tab-navigator组件的基本使用示例代码
- react-native 圆弧拖动进度条实现的示例代码
- react native 获取地理位置的方法示例
- React-Native中禁用Navigator手势返回的示例代码
- react-native 中配置极光推送 Android关键详细的说明与截图代码 找不到类Bundle/JPushInterface
- 从零开始 React Native (5) React 数据传递_表单_函数复用_动画