Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Facebook ShareDialog.canShow() == false on Android

Facebook's Sharing on Android documentation tells us to show the ShareDialog with the following code snippet:

if (ShareDialog.canShow(ShareLinkContent.class)) {
    ShareLinkContent linkContent = new ShareLinkContent.Builder()
            .setContentTitle("Hello Facebook")
            .setContentDescription(
                    "The 'Hello Facebook' sample  showcases simple Facebook integration")
            .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
            .build();

    shareDialog.show(linkContent);
}

My question is why do we need to check ShareDialog.canShow()? In what scenarios would this possibly return false and do we need to handle this scenario? The example code would just fail silently not tell the user anything.

like image 755
Stephen Kidson Avatar asked Sep 17 '15 18:09

Stephen Kidson


1 Answers

I spent a lot of time yesterday trying to debug an error related to this. Facebook's docs are poor in this sense and when there is an error related to this canShow() method, it just fails silently, no logging at all.

So, to answer your question:

My question is why do we need to check ShareDialog.canShow()? In what scenarios would this possibly return false and do we need to handle this scenario?

Based on the scenario I faced: when the user doesn't have the Facebook app installed on their device and you want to share photos (SharePhotoContent) or videos (ShareVideoContent), canShow() will return false. The reason is that Facebook SDK's WebView version doesn't support sharing this kind of content.

I found this out debugging their FacebookActivity class, on the handlePassThroughError() method. The (not logged) error message is:

"Unable to show the provided content. This typically means that the Facebook app is not installed or up to date. If showing via the Web, this could mean that the content has properties that are not supported via this channel."

So, what should we do when canShow() returns false?

It depends on your scenario. Possible solutions would be:

  1. Show a dialog (or SnackBar) telling the user that they need to install the Facebook app to be able to share this kind of content;
  2. Request an authentication token, log the user using the Facebook SDK, and share the content using your own dialog and calling Facebook API directly.

Possible solutions for Facebook would be include this on their documentation or log this error on LogCat.

Hope it helps!

like image 69
leocadiotine Avatar answered Oct 21 '22 01:10

leocadiotine