1. 程式人生 > >react native 中函數Share示例與說明

react native 中函數Share示例與說明

dia android中 truct ror com title radi ops res

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


* ***/

react native 中函數Share示例與說明