Facebook Authentication in a Uiwebview Does Not Redirect Back to Original Page on My Site Asking for Auth

Not redirecting to Facebook comments in UIWebView after login

I suspect the problem you're having is related to this issue: Facebook authentication in a UIWebView does not redirect back to original page on my site asking for auth

Specifically, the standard Facebook web login process launches a new browser window dialog, and dispatches a message back to the opener to indicate login success for the redirect to occur.

Quoting a passage in the linked SO, "UIWebView doesn't support multiple windows so it can't postMessage back to your original page since it's no longer loaded."

Facebook Javascript login in native ios6 UIWebView (WORKS ON SAFARI)

Did it!

It kinda of a hack, but the js facebook sdk login on UiWebView at iOS 6 finally works.

How it could be done? It is a pure JS + Facebook JS SDK + UIWebView Delegate handling functions solution.

JS - First step)

a login button (to connect with facebook) calls this function example, that will trigger Face JS login/oauth dialogs:

function loginWithFacebookClick(){

FB.login(function(response){
//normal browsers callback
});

}

JS - Second step)

add a authResponseChange listener every time user loads the webpage ( after FB.init() ) to catch user's connected status:

FB.Event.subscribe('auth.authResponse.Change', function(response){
//UIWebView login 'callback' handler
var auth = response.authResponse;
if(auth.status == 'connected'){
//user is connected with facebook! just log him at your webapp
}
});

AND with app's UIWebView delegate functions you can handler facebook oauth responses

Objective C - Third step)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = [[request URL] absoluteString];

//when user status == connected (has a access_token at facebook oauth response)
if([url hasPrefix:@"https://m.facebook.com/dialog/oauth"] && [url rangeOfString:@"access_token="].location != NSNotFound)
{
[self backToLastPage];
return NO;
}

return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

NSString *url = [[webView.request URL] absoluteString];

if([url hasPrefix:@"https://m.facebook.com/dialog/oauth"])
{
NSString *bodyHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

//Facebook oauth response dead end: is a blank body and a head with a script that does
//nothing. But if you got back to your last page, the handler of authResponseChange
//will catch a connected status if user did his login and auth app
if([bodyHTML isEqualToString:@""])
{
[self backToLastPage];
}
}

}

So, when 'redirect' user to the last loaded page, the second step is going to handler user action at facebook login dialogs.

If I got too fast with this answer, please ask me!
Hope it helps.

Android Webview and Facebook Login Not working

I was able to find a solution based on a previous question on stackoverflow.com

Making facebook login work with an Android Webview

Here is a link to my MainActivity : http://pastebin.com/KdzfhqDJ



Related Topics



Leave a reply



Submit