做到了!
这有点像 hack,但是 iOS 6 上 UiWebView 上的 js facebook sdk 登录终于可以工作了。
怎么可能做到?它是一个纯 JS + Facebook JS SDK + UIWebView Delegate 处理功能的解决方案。
JS - 第一步)
登录按钮(用于连接 facebook)调用此函数示例,这将触发 Face JS 登录/oauth 对话框:
function loginWithFacebookClick(){
FB.login(function(response){
//normal browsers callback
});
}
JS - 第二步)
每次用户加载网页时(在 FB.init() 之后)添加一个 authResponseChange 监听器以捕获用户的连接状态:
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
}
});
并且使用应用程序的 UIWebView 委托函数,您可以处理 facebook oauth 响应
目标 C - 第三步)
- (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];
}
}
}
因此,当“重定向”用户到最后加载的页面时,第二步是在 facebook 登录对话框中处理用户操作。
如果我对这个答案太快了,请问我!希望能帮助到你。