Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect user cancelled share when using fb.ui

I'm using the documentation provided over here with the following code. The share dialog comes up correctly. The problem is that I'm not able to differentiate between "Cancel" and "Post" actions that the user takes on the dialog. I'd imagine this would be a part of the response.

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/',
}, function(response){
    if (response && !response.error_code) {
        console.log(response);
    } else {
        alert('Error while posting.');
    }
});

edit: output from the console isn't doesn't provide any way of knowing

Cancel - Object {e2e: "{"submit_0":1401181811121}"} 
Post - Object {e2e: "{"submit_0":1401181815112}"} 
like image 900
Abhinav Gujjar Avatar asked May 27 '14 08:05

Abhinav Gujjar


2 Answers

I tested this, and apparently there's some info in the response object you could use to determine if the dialog was cancelled.

Code

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});

Output upon cancellation:

{error_code: 4201, error_message: "User+canceled+the+Dialog+flow", e2e: "{"submit_0":1401188820613}"} 

So, I guess you could check for cancellaction like this:

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else if (response && response.error_code === 4201) { //Cancelled
        console.log("User cancelled: "+decodeURIComponent(response.error_message));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});

Unfortunately, FB.Events.subscribe() doesn't offer an Event for the Cancallation of this dialog: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.0

like image 116
Tobi Avatar answered Oct 23 '22 22:10

Tobi


This is intentionally so as to dissuade developers from using posting as a gating mechanism. It should be up to the person to choose whether to post or not, it should not be a requirement of the app.

like image 2
Sean Kinsey Avatar answered Oct 23 '22 22:10

Sean Kinsey