简体   繁体   中英

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

I am looking for a solution about running FB.login() at a native iOS 6 app with UIWebView. I already debugged a lot and did not found the solution yet.

The problem is: When a call fb.login, my app opens a new UIWebView to handle it (like safari opens a new tab). Facebook Login appears but when it logs, a blank page appears and nothing happens (neither in root UIWebView)... anyone knows the solution to complete fb.login js flow?

Thanks guys!

PS: Links that does not helps--

How can I get UIWebView to open Facebook login page in response to the OAuth request on iOS 5 and iOS 6?

https://stackoverflow.com/questions/13387272/fb-login-issue-with-ios-6

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM