我也不得不处理这个问题。
调用对话框方法时,您发送一个应符合 FBDialogDelegate 的委托,该委托有一个方法,当对话框因错误而无法加载时调用。但在应用程序未经授权的情况下,对话框会向用户显示登录屏幕,但在设置用户和密码后,会出现第二个表单,让用户知道发生了错误。委托也被调用,但收到的错误只是表明他的方法失败了,没有确切的原因,甚至没有错误号。这个方法应该在正确的错误被调用之前,所以应用程序可以采取相应的行动。
所以我找到了解决方法,也许这不是最好的方法,但它确实有效。如果应用未经用户授权,您通过请求对 Facebook 图形 api 进行的任何调用都将失败。所以我所做的是在调用提要对话框方法之前检查它。
在您需要测试应用程序是否仍被授权的地方添加以下行:
if ([facebook isSessionValid])
//isSessionValid only checks if the access token is set, and the expiration date is still valid. Lets make a call and see if we really are authorized to post to this user from this app.
[facebook requestWithGraphPath:@"me" andDelegate:self];
else
//authorize Facebook connect
这只会调用返回用户基本信息的方法。如果一切正常,将从委托中调用以下方法:
- (void)request:(FBRequest *)request didLoad:(id)result
{
//Everything is ok. You can call the dialog method. It should work.
}
如果应用未经用户授权,将调用委托的以下方法:
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;
{
NSString *type = [[[error userInfo] objectForKey:@"error"] objectForKey:@"type"];
if (type)
{
if ([type isEqualToString:@"OAuthException"]) //aha!
{
//user has unauthorized the app, lets logout from Facebook connect. Also clear the access and expiration date tokens
[facebook logout:self];
//Call the authorize method again. Or let the user know they need to authorize the app again.
}
}
}
所以,正如我之前所说,这不是最好的方法,但可以完成工作。希望 Facebook 将添加一种方法来检查这种特定情况,或者向代理添加一种新方法来处理未经授权的应用程序问题。