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.
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:
Possible solutions for Facebook would be include this on their documentation or log this error on LogCat.
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With