react native 中函数Share示例与说明

/**
* 函数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


* ***/
原文地址:https://www.cnblogs.com/zhuyupingit/p/7667934.html