我想通过WhatsApp直接与特定人共享PDF。还有其他方法吗?
我在下面尝试过,但并未直接分享给特定的人。
import Share from 'react-native-share';
Share.shareSingle(options);
我也尝试过此操作,但是它不能共享PDF,只能共享文本。
Linking.openURL(`whatsapp://send?text=${options}&phone=${options.phoneNumber}`);
更多代码:
filePath = resp.path();
(Platform.OS === 'android') ? filePath = 'file://' + filePath : filePath;
let options = {
type: 'application/pdf',
url: filePath,
title: "Share title",
message: "Share message",
subject: "Share subject",
social: Share.Social.WHATSAPP,
phoneNumber: "+60 12-345 6789"
};
安装react-native-fetch-blob
在RNFetchBlob配置中设置特定路径
static sharePDFWithIOS(fileUrl, type) {
let filePath = null;
let file_url_length = fileUrl.length;
const configOptions = {
fileCache: true,
path:
DIRS.DocumentDir + (type === 'application/pdf' ? '/SomeFileName.pdf' : '/SomeFileName.png') // no difference when using jpeg / jpg / png /
};
RNFetchBlob.config(configOptions)
.fetch('GET', fileUrl)
.then(async resp => {
filePath = resp.path();
let options = {
type: type,
url: filePath // (Platform.OS === 'android' ? 'file://' + filePath)
};
await Share.open(options);
// remove the image or pdf from device's storage
await RNFS.unlink(filePath);
});
}
static sharePDFWithAndroid(fileUrl, type) {
let filePath = null;
let file_url_length = fileUrl.length;
const configOptions = { fileCache: true };
RNFetchBlob.config(configOptions)
.fetch('GET', fileUrl)
.then(resp => {
filePath = resp.path();
return resp.readFile('base64');
})
.then(async base64Data => {
base64Data = `data:${type};base64,` + base64Data;
await Share.open({ url: base64Data });
// remove the image or pdf from device's storage
await RNFS.unlink(filePath);
});
}