从React-Native应用程序在Facebook应用程序中分享URL作为帖子



我有一个react原生应用程序,从中我想分享我的网站在Facebook帖子的URL。我现在的代码是:

import Share from "react-native-share";
const shareToFacebook = async () => {
const shareOptions = {
social: Share.Social.FACEBOOK,
message: "Test message",
url: "https://example.com/",
};
try {
const ShareResponse = await Share.shareSingle(shareOptions);
console.log(ShareResponse);
} catch (error) {
console.log("Error =>", error);
}
};

但是这个解决方案,在浏览器中打开Facebook。有没有办法打开实际的Facebook应用程序?

Facebook提供了一个用于分享帖子的SDK,您可以使用react-native-fbsdk-next库。它显示了一个应用内模态。我认为显示应用内模式比打开Facebook应用程序或打开浏览器更好,因为有些人的手机上没有Facebook。

您可以在安装库之前测试共享功能,遵循以下步骤:

# clone the react-native-fbsdk-next
git clone https://github.com/thebergamo/react-native-fbsdk-next.git
# go to the test project
cd RNFBSDKExample
# install dependencies
npm install
# run the project
npm run android

示例代码(来自README页):

// ...
import { ShareDialog } from 'react-native-fbsdk-next';
// ...
// Build up a shareable link.
const shareLinkContent = {
contentType: 'link',
contentUrl: "https://facebook.com",
contentDescription: 'Wow, check out this great site!',
};
// ...
// Share the link using the share dialog.
shareLinkWithShareDialog() {
var tmp = this;
ShareDialog.canShow(this.state.shareLinkContent).then(
function(canShow) {
if (canShow) {
return ShareDialog.show(tmp.state.shareLinkContent);
}
}
).then(
function(result) {
if (result.isCancelled) {
console.log('Share cancelled');
} else {
console.log('Share success with postId: '
+ result.postId);
}
},
function(error) {
console.log('Share fail with error: ' + error);
}
);
}

阅读更多关于Facebook共享功能。

试试react-native-share ^9.0.2

Share.shareSingle({
title: "TITLE",
message: "MESSAGE",
url: "URL",
social: Share.Social.FACEBOOK,
type: 'url',
})
.then((res) => { console.log(res) })
.catch((err) => { err && console.log(err); });