/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow */ import React, { Fragment, Component } from 'react'; import { TouchableOpacity, SafeAreaView, ScrollView, StyleSheet, StatusBar, Platform, View, Text, Alert } from 'react-native'; import { Header, Colors } from 'react-native/Libraries/NewAppScreen'; import * as WeChat from 'react-native-wechat'; export default class App extends Component { shareOptions = { title: 'playground', description: '微信分享测试', thumbImage: 'https://i.loli.net/2019/09/03/62FauzAY37gsEXV.png', type: 'news', webpageUrl: 'https://github.com/little-snow-fox/react-native-wechat-lib' } constructor(props) { super(props); this.state = { apiVersion: null, isWXAppInstalled: false, wxAppInstallUrl: null, isWXAppSupportApi: false } } handleOpenApp () { if (this.state.isWXAppInstalled) { return WeChat.openWXApp(); } else { Alert.alert('没有安装微信,请安装之后重试'); } } handleShareToSession () { if (this.state.isWXAppInstalled) { WeChat.shareToSession(this.shareOptions).catch((error) => { Alert.alert(error.message); }); } else { Alert.alert('没有安装微信,请安装之后重试'); } } handleShareToMoment () { if (this.state.isWXAppInstalled) { WeChat.shareToTimeline(this.shareOptions).catch((error) => { Alert.alert(error.message); }); } else { Alert.alert('没有安装微信,请安装之后重试'); } } async componentDidMount() { try { WeChat.registerApp('your wexin AppID'); // Replace with your AppID this.setState({ apiVersion: await WeChat.getApiVersion(), wxAppInstallUrl: Platform.OS === 'ios' ? await WeChat.getWXAppInstallUrl(): null, isWXAppSupportApi: await WeChat.isWXAppSupportApi(), isWXAppInstalled: await WeChat.isWXAppInstalled() }); } catch (e) { console.error(e); } } render() { const { apiVersion } = this.state; return (
ApiVersion: { apiVersion } this.handleOpenApp()}> 打开微信 this.handleShareToSession()}> 分享至微信好友 this.handleShareToMoment()}> 分享至朋友圈 ); } }; const styles = StyleSheet.create({ scrollView: { backgroundColor: Colors.lighter }, body: { backgroundColor: Colors.white }, sectionContainer: { marginTop: 32, paddingHorizontal: 24 }, highlight: { fontSize: 24, fontWeight: '600', color: Colors.black, textAlign: 'center' }, button: { flex: 1, margin: 10, borderWidth: 1, borderRadius: 30, paddingVertical: 12, alignItems: 'center', justifyContent: 'center', borderColor: Colors.black } });