15

I'm making an iOS Webapp (i.e. an HTML page that runs in standalone mode - none of the Safari chrome - when the bookmark of the page is added to the homescreen).

I have a button that onclick calls FB.login(). When in standalone mode, the webapp redirects to the facebook login page (as expected), however afterwards, I get stuck on a white screen (I don't get asked to authenticate since I already have, but I imagine that the authentication screen would happen before the white screen) and don't get put back into the webapp.

The same flow within the Safari app works as expected. Clicking login calls FB.login(), which opens a new page where you are asked to login to Facebook, once you login and authenticate the app, that page closes and you are put back into the original page.

It seems that in standalone mode, there's a problem with the "second page" with the FB login closing and redirecting back to the app (it doesn't). And the flow is broken.

Is there any sort of workaround for this?

Thanks,
-Esa

Esaevian
  • 1,707
  • 5
  • 18
  • 30
  • Just to share my experience, if anyone comes here again: i solved by simply replacing the button with a simple html link to: `https://www.facebook.com/dialog/oauth/?client_id=[MY_APP_ID]&redirect_uri=[URL_TO_REDIRECT_AFTER_PERMISSIONS]&scope=[COMMA_SEPARATED_SCOPES]` – Stormsson Sep 10 '14 at 12:32

2 Answers2

9

You can try the workaround below. It worked for me. In mobile, it redirects to client side authentication url.

var isMobile = false;
try {
    isMobile = (window.location.href == top.location.href && window.location.href.indexOf("/mobile/") != -1);
} catch (e) {}
if (!isMobile) {
    FB.login();
} else {
    var permissionUrl = "https://m.facebook.com/dialog/oauth?client_id=" + appId + "&response_type=code&redirect_uri=" + redirectPage + "&scope=" + permissions;
    window.location = permissionUrl;
    return;
}
Baris Aydinoglu
  • 364
  • 3
  • 6
  • Thanks! I actually did something similar, inspired by this: `FB.login(function(response) { /*stuff*/ }, {redirect_uri: redirect, scope: permissions});` It throws me back to the app. Only issue is that it doesn't save the state of my app, and has the user start over, but it works just fine otherwise. Also I replaced the isMobile logic with my own (checking the useragent, this is a mobile-only site, so I don't need any fallbacks for the desktop) – Esaevian Jun 28 '12 at 19:05
  • I was also using the undocumented redirect_uri option in standalone mode, but it recently stopped working for me, leaving me with the blank white screen problem again. I'm now using the above method, switching the window.location to facebook's oauth endpoint. The redirect works, though the web app reloads from scratch. I can live with that. – Mike Dec 12 '12 at 18:32
  • this worked for me, thanks, I was getting pretty frustrated before i found this! – asutherland Dec 09 '13 at 20:09
  • I prefer @Max_Carlson solution. No need for a complex logic. FB.Login() works fine on desktop or Mobile. Beside on **Standalone mode**. See his code in his answer. Simple & Sweet. – neoswf Apr 08 '14 at 23:52
  • 1
    How do I get the response object after this redirection on standalone browsers? – István Pálinkás Mar 10 '15 at 11:03
  • hello, How can I able to get email,token & user_id from code response? – user2526811 Apr 06 '15 at 12:10
  • thanks, it works fine. after the redirect happens and you identify the access token, you have to load the FB SDK, invoke FB.getLoginStatus, and handle the response the same way as you do with FB.login(). for me it's a bit more complicated, because it's wrapped in a complex application with history handling and such, but the basic logic is as above. – szajmon Apr 22 '15 at 16:17
8

I used the following to detect if I'm in home screen mode and do the right thing accordingly:

if ("standalone" in navigator && navigator.standalone) {
  var permissionUrl = "https://m.facebook.com/dialog/oauth?client_id=" + appId + "&response_type=code&redirect_uri=" + window.location + "&scope=" + app_permissions;
  window.location = permissionUrl;
} else {
  FB.login(
    function(response) {
      ...
    },
    {scope: app_permissions}
  );
}
Max Carlson
  • 81
  • 1
  • 1