11

The following FB.Login function works fine in desktop Chrome, FF, and Safari. But in mobile Safari (tested on an iPhone 4S), it hangs and does not return to the FB.login callback. I can also see this in the console when I use Safari and set the User Agent to "Safari iOS 4.3.3 - iPhone".

Is it because mobile Safari blocks popups? (FB.login triggers a popup dialog).

How do I fix this? Thanks.

function Login(returnLink) {
        FB.login(function(response) {
                    if(response.status === 'connected') {
                        console.log('User is now FB logged in.');
                        // now log them into my site
                        encodedReturnLink = encodeURIComponent(returnLink);
                        window.location = location.protocol + '//' + location.host + "/login?returnUrl=" + encodedReturnLink;
                    }
                    else {
                        console.log('User did not fully authorize after clicking FB login button.');
                    }
                },
                {scope : 'email, publish_actions, publish_stream'}
        );
}
2
  • Are you calling this immediately after user interaction (click/tap/submit) or from some delayed code? May 2, 2012 at 4:36
  • 1
    Running into the same problem. Did you find a solution to this?
    – Jaffer
    Dec 5, 2012 at 8:03

3 Answers 3

7

I was trying to run FB.Login automatically on page load inside window.fbAsyncInit and it wasn't working in Safari on iOS. It turns out Safari was blocking the popup window triggered by the FB.Login call. Safari and Chrome on the desktop worked fine (allowed the popup).

I found the only way to work-around this was to trigger the FB.Login call in response to user interaction (eg. Tap or Click). This worked for me:

HTML

<a href="#" id="fbLogin">Login with Facebook</a>

JavaScript (jQuery)

$('#fbLogin').click(function(){
    FB.login(function(response){
        // Do something...
    });
});

Tested on Safari (iOS 8.0).

4
  • +1 for giving correct solution. It removed my headache. Thanks a lot.
    – Neeraj
    Oct 15, 2014 at 14:17
  • 1
    Safari seems pretty sensitive to blocking pop ups. If you delay at all (even by waiting a tiny amount for other callbacks) it seems to trigger the block. Jan 8, 2015 at 7:18
  • @wireblue is this still the only way to trigger the login in current versions?
    – gapvision
    Jul 14, 2015 at 9:24
  • @gapvision, I have not tested this recently so I'm not sure.
    – Wireblue
    Jul 17, 2015 at 0:42
0

It's definitely not a matter of blocking popups. Though I have seen a scenario in which the login works when called directly, but not when the result of an ajax callback.

1
  • well, but this is indeed a matter of blocking popups, as it is obviously blocked when called as callback and not when called directly?
    – gapvision
    Jul 14, 2015 at 9:26
0

Double-check Mobile Site URL in your Facebook app settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.