我目前正在尝试使用 React Native 构建一个应用程序,用户可以在其中输入数据,生成二维码,然后共享相同的二维码。
输入数据并生成 QR 码按预期工作。然而,分享是让我挣扎的原因。目前我将数据保存为普通变量,并使用“react-native-qrcode-svg”显示二维码。对于共享部分,我使用“react-native-share”。
后者给了我这个错误:
TypeError: null is not an object (evaluating '_reactNative.NativeModules.RNShare.FACEBOOK')
我的代码:
export default function App() {
return (
<View style={styles.container}>
<QRCode
value={JSON.stringify({test: 'testdata'})}
getRef={c => (this.svg = c)}
/>
<TouchableOpacity onPress={this.saveQRCode}>
<View style={styles.instructions}>
<Text>Share QR code</Text>
</View>
</TouchableOpacity>
</View>
);
}
saveQRCode = () => {
this.svg.toDataURL(this.callback);
};
callback = (dataURL) => {
console.log(dataURL);
let shareImageBase64 = {
title: 'React Native',
url: `data:image/png;base64,${dataURL}`,
subject: 'Share Link', // for email
};
Share.open(shareImageBase64).catch(error => console.log(error));
}
任何帮助是极大的赞赏。