正在尝试构建一个React Native应用程序,该应用程序在共享菜单( Android的共享操作,iOS的共享扩展)中插入菜单项,并在应用程序中接收共享项目。有这样的组件吗?如果没有,构建一个组件的最佳方式是什么?
发布于 2016-03-14 13:39:07
我为此实现了一个模块:https://www.npmjs.com/package/react-native-share-menu (目前只适用于安卓系统)。
这就是它的用法:
安装模块:
npm i --save react-native-share-menu
在android/settings.gradle中:
...
include ':react-native-share-menu', ':app'
project(':react-native-share-menu').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share-menu/android')
在android/app/build.gradle中:
...
dependencies {
...
compile project(':react-native-share-menu')
}
注册模块(MainActivity.java格式):
import com.meedan.ShareMenuPackage; // <--- import
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ShareMenuPackage() // <------ add here
);
}
......
}
示例:
import React, {
AppRegistry,
Component,
Text,
View
} from 'react-native';
import ShareMenu from 'react-native-share-menu';
class Test extends Component {
constructor(props) {
super(props);
this.state = {
sharedText: null
};
}
componentWillMount() {
var that = this;
ShareMenu.getSharedText((text :string) => {
if (text && text.length) {
that.setState({ sharedText: text });
}
})
}
render() {
var text = this.state.sharedText;
return (
<View>
<Text>Shared text: {text}</Text>
</View>
);
}
}
AppRegistry.registerComponent('Test', () => Test);
发布于 2015-11-18 16:33:51
您可以使用内置组件:https://facebook.github.io/react-native/docs/actionsheetios.html#content
或者,您可以使用此组件,它可以接受您想要的任何视图,并使其看起来像iOS share组件:
https://github.com/eyaleizenberg/react-native-custom-action-sheet
这些都是为iOS设计的。对于安卓(以及iOS),你可以使用这个:https://github.com/EstebanFuentealba/react-native-share
发布于 2016-09-13 02:44:25
您可以使用0.33中引入的新Share api。实际上我在这里有一个关于如何做到这一点的视频:http://codecookbook.co/post/how-to-share-text-from-your-react-native-app/
https://stackoverflow.com/questions/33766677
复制相似问题