Share an image with react-native-fbsdk?

六眼飞鱼酱① 提交于 2019-12-13 12:27:29

问题


I'm trying to share an image to Facebook in React Native through the react-native-fbsdk package.

I have the following, copied almost completely from the docs, but I can't figure out how to format the imageUrl, I keep getting errors saying the SharingContent is invalid.

What am I doing wrong here?

const sharePhotoContent = {
  contentType: 'photo',
  photos: [
    {
      imageUrl: "./local/image/path.png",
      userGenerated: false,
      caption: "Hello World"
    }
  ]
};

ShareDialog.canShow(sharePhotoContent).then(
  function(canShow) {
    if (canShow) {
      return ShareDialog.show(sharePhotoContent);
    }
  }
).then(
  function(result) {
    if (result.isCancelled) {
      AlertIOS.alert('Share cancelled');
    } else {
      AlertIOS.alert('Share success with postId: ' + result.postId);
    }
  },
  function(error) {
    AlertIOS.alert('Share fail with error: ' + error);
  }
);

回答1:


You can use the react-native-image-picker https://github.com/marcshilling/react-native-image-picker to get the uri of the image and use it create a SharePhotoContent.

Also note that you need to have the facebook app installed in order to share an image.




回答2:


This is working fine for me 

  shareLinkWithShareDialog() {

let shareLinkContent = {

contentType: 'link',

contentUrl: 'https://www.facebook.com/images/fb_icon_325x325.png',

contentDescription: 'Facebook sharing is easy!'
      }

 ShareDialog.canShow(shareLinkContent).then((canShow) => {

if (canShow) return ShareDialog.show(shareLinkContent);

 }

).then((result) => {

 if (result.isCancelled) {

  alert('Share cancelled');

 } else {

   alert('Share success with postId: ' + result.postId);

   }

  },

  function(error) {

  alert('Share fail with error: ' + error);
      }
    );
  }


来源:https://stackoverflow.com/questions/37061266/share-an-image-with-react-native-fbsdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!