如何让 UIWebView 打开 Facebook 登录页面以响应 iOS 5 和 iOS 6 上的 OAuth 请求? [英] How can I get UIWebView to open Facebook login page in response to the OAuth request on iOS 5 and iOS 6?

查看:13
本文介绍了如何让 UIWebView 打开 Facebook 登录页面以响应 iOS 5 和 iOS 6 上的 OAuth 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有:
(1) 具有 Facebook OAuth 功能的基于 Facebook API 的 Web 应用程序(FB Web 应用程序")
(2) iPad上基于UIWebView的浏览器(浏览器")

We have:
(1) Facebook API-based web application with Facebook OAuth functionality ("the FB web app")
(2) UIWebView-based browser on iPad ("the Browser")

我们的目标是在 iPad 上基于 UIWebView 的浏览器 (2) 中打开 Facebook 登录页面以登录到 FB 网络应用程序 (1).

Our objective is to open the Facebook Login page to sign in to the FB web app (1) inside the UIWebView-based Browser (2) on iPad.

这里有一个类似的问题:
http://facebook.stackoverflow.com/questions/11337285/no-longer-able-to-login-to-ios-app-via-oauth-the-page-requested-was-not-found

There is a somewhat similar issue here:
http://facebook.stackoverflow.com/questions/11337285/no-longer-able-to-login-to-ios-app-via-oauth-the-page-requested-was-not-found

但是,在用户在 Facebook 表单中输入登录名和密码后,就会出现该问题.我们的问题是我们无法首先显示 Facebook 登录表单.按照该问题的建议,将应用类型从Web"更改为Native/Desktop"并没有帮助.

However, the issue of that question happens after the user enters login and password into the Facebook form. Our problem is that we cannot get the Facebook login form displayed in the first place. Changing the app type to from "Web" to "Native/Desktop", as suggested in that question, did not help.

步骤:
1.用这个UIWebView浏览器
打开我们的网页(简单的HTML页面)2. 点击此页面上的FB web app"启动按钮
3. OnClick JavaScript 尝试启动 OAuth,这应该会打开 Facebook 的登录屏幕以登录到 FB Web 应用程序

Steps:
1. Open our web page (simple HTML page) with this UIWebView Browser
2. Click on "FB web app" launch button on this page
3. OnClick JavaScript tries to initiate OAuth, which should open the login screen of Facebook to sign in to the FB web app

当前结果(问题):
在 iOS 5.+ 和 iOS 6.+ 设备上
- 我们的网页保持不变
- 未显示 Facebook 登录页面(仍显示我们的网页)
在 iOS 4.3 上(按预期工作):
- Facebook 登录页面在浏览器的相同 UIWebView 对象中打开(替换我们的网页)

Current outcome (issue):
On iOS 5.+ and iOS 6.+ devices
- Our web page stays unchanged
- Facebook login page is NOT shown (our web page is still displayed)
On iOS 4.3 (works as expected):
- the Facebook login page is opened in the same UIWebView object of the Browser (replaces our web page)

预期结果:
- 显示Facebook登录页面,用户可以输入Facebook登录&密码
- 如果在 iPad 上的 Safari 浏览器中启动,则适用于 iOS 5.+ 和 iOS 6.+.Facebook 登录页面在单独的标签页中打开(相比之下,UIWebView 中没有单独的标签页)

Expected outcome:
- Facebook login page is displayed, and the user can enter Facebook login & password
- Works on iOS 5.+ and iOS 6.+ if launched in Safari browser on iPad. Facebook login page is opened in a separate tab (in contrast, there are no separate tabs in UIWebView)

问题:如何让 UIWebView 响应 iOS 5+ 和 iOS 6+ 上的 OAuth 请求打开 Facebook 登录页面?

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

更多技术细节:

我们在

-(BOOL)webView(UIWebView*)webView shouldStartLoadWithRequest(NSURLREquest*)request navigationType:…   

我们注意到正确"和不正确"行为的日志存在一些差异.执行流程如何寻找我:

And we notice some difference in logs for "correct" and "incorrect" behaviors. Here how execution flows look for me:

首先,我按下FB Web App"启动按钮启动 OAuth,然后某些情况下

Firstly, I press "FB Web App" launch button to initiate OAuth, then some cases go

iOS 4.3,正确"
请求到 www.facebook.com/dialog/oauth?...
请求 fbwebapp.com
请求到 m.facebook.com/login.php?....
--这里出现facebook登录

iOS 4.3, "correct"
request to www.facebook.com/dialog/oauth?...
request to fbwebapp.com
request to m.facebook.com/login.php?....
--here facebook login appears

iOS 5.0,不正确1"
请求到 www.facebook.com/dialog/oauth?...
请求 fbwebapp.com
向 m.facebook.com/login.php 提出请求?...

iOS 5.0, "incorrect1"
request to www.facebook.com/dialog/oauth?...
request to fbwebapp.com
request to m.facebook.com/login.php?...

那么可能是
-- 很多 m.facebook.com/login.php?...接下来...在参数中
其次是sqlite错误
--现在我从 facebook 看到对不起,出了点问题"页面(这是我第一次遇到它)

Then it may be
--a lot of m.facebook.com/login.php?...with next… in parameters
followed by sqlite error
--right now I see "Sorry, something went wrong" page from facebook (it’s a first time at all I encounter it)

iOS 6.0不正确2"
请求到 www.facebook.com/dialog/oauth?...
请求 fbwebapp.com

iOS 6.0 "incorrect2"
request to www.facebook.com/dialog/oauth?...
request to fbwebapp.com

-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error 被调用,错误代码为 -999

-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error is invoked with error code -999

您可以看到该行为绝对取决于 iOS 版本.但常见的情况是在获取 m.facebook.com/login.php.. URL 的步骤中发生错误.但这就是我们所能检测到的.

You can see that behavior definitely depends on iOS version. But common case is that error happens on the step of obtaining m.facebook.com/login.php.. URL. But that’s all that we can detect.

我们整天都在用头撞墙寻找解决方案.绝望.
您能否帮助我们在 UIWebView 中打开 Facebook 登录页面以响应 OAuth?

We’re banging our heads against that wall for the whole day looking for solutions. Hopelessly.
Can you help us get the Facebook Login page opened in the UIWebView in response to OAuth?

推荐答案

只需使用:这段代码

if (![FBSDKAccessToken currentAccessToken]) 
{   
    FBSDKLoginManager *manager = [[FBSDKLoginManager alloc]init];
    manager.loginBehavior = FBSDKLoginBehaviorWeb;
    [manager logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] handler:
     ^(FBSDKLoginManagerLoginResult *result, NSError *error) {

         NSLog(@"result.token: %@",result.token.tokenString);  
         NSLog(@"%@",result.token.userID);   
         NSLog(@"%hhd",result.isCancelled);     
     }];
}

//这里 manager.loginBehavior = FBSDKLoginBehaviorWeb; 是你在 UIWebview

这篇关于如何让 UIWebView 打开 Facebook 登录页面以响应 iOS 5 和 iOS 6 上的 OAuth 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆