FaceBook iOS - 检查我的Facebook应用程序是否已被授权 [英] FaceBook iOS - check if my facebook app is allready authorized

查看:809
本文介绍了FaceBook iOS - 检查我的Facebook应用程序是否已被授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何检查我的FaceBook应用程序是否已经被用户的帖子授权,无法找到任何信息。



我正在使用:

  Facebook * facebook = [[Facebook alloc] initWithAppId:@1234567]; 
[facebook authorize:[NSArray arrayWithObjects:@read_stream,@offline_access,nil] delegate:self];

一个对话框弹出要求我授权该应用程序,完成后,我可以做到a:

  [facebook dialog:@feedandDelegate:self]; 

在该应用程序上发布笔记。



但是,如果用户阻止或删除应用程序,我想在显示发布对话框之前再次进行授权,则无法在调用授权之前找到获取该类信息的方法。


$



谢谢。

解决方案

我也处理这个问题。



当调用dialog方法时,您发送一个应该符合FBDialogDelegate的委托,该对象有一个方法,当由于错误而无法加载对话框时调用该方法。但是在应用程序未经授权的情况下,该对话框将显示用户的登录屏幕,但在设置用户和密码后,将显示第二个表单,让用户知道发生错误。代表也被调用,但收到的错误只是表示他的方法失败,没有确切的原因,甚至错误号。这个方法应该被调用正确的错误,在任何事情之前,所以应用程序可以相应地行动。



所以我找到了一个工作,也许这不是最好的方法,但它肯定有效。您通过请求对Facebook图形API进行的任何呼叫将在用户未经授权的情况下失败。所以我做的是在调用feed对话框方法之前检查一下。



添加以下行,您需要测试应用程序是否仍然授权:

 code> if([facebook isSessionValid])
// isSessionValid仅检查访问令牌是否设置,到期日期仍然有效。让我们打电话,看看我们是否真的被授权从这个应用程序发布给这个用户。
[facebook requestWithGraphPath:@meandDelegate:self];
else
//授权Facebook连接

这将只是调用方法从用户返回基本信息。如果一切正常,将从委托中调用以下方法:

   - (void)request:(FBRequest *)request didLoad:(id)result 
{
//一切都可以。可以调用dialog方法。应该工作
}

如果应用程序已被用户未经授权,则来自代理的以下方法将被调用:

   - (void)请求:(FBRequest *)请求didFailWithError:(NSError *)错误; 
{
NSString * type = [[error userInfo] objectForKey:@error] objectForKey:@type];

if(type)
{
if([type isEqualToString:@OAuthException])// aha!
{
//用户未经授权的应用程序,可以从Facebook连接注销。还清除访问和到期日期令牌
[facebook logout:self];


//再次调用授权方法。或者让用户知道他们需要再次授权该应用程序。
}
}
}

所以,正如我之前所说不是最好的方式,而是完成工作。希望Facebook会添加一种方法来检查此具体情况,或者向代理人添加一种处理未经授权的应用程序问题的新方法。


My question is how to check if my FaceBook app is already authorized for posts by the user, can't find any info on that.

I'm using:

 Facebook* facebook = [[Facebook alloc] initWithAppId:@"1234567"];
 [facebook authorize:[NSArray arrayWithObjects:@"read_stream", @"offline_access",nil] delegate:self];

A dialog pops up asking me to authorize the app, when done i'm all fine, can do a:

 [facebook dialog:@"feed" andDelegate:self];

to post notes on that app.

But, if the user blocks or removes the app i want to do the authorize again before showing the dialog for posting, can't find a way of getting that kind of info before calling authorize.

Any help is appreciated.

Thanks.

解决方案

I had to deal with this issue too.

When calling the dialog method, you send a delegate that should conform to FBDialogDelegate, which has a method that is called when the dialog fails to load due an error. But in the case the app has been unauthorized, the dialog shows a login screen to the user, but after setting the user and password, a second form appears, letting the user know that an error has occurred. The delegate is also called, but the error received just states that he method has failed with no exact reason why, or even an error number. This method should be called with the correct error, before anything, so the application could act accordingly.

So I found a work around, maybe this is not the best way, but it certainly works. Any call that you do to the Facebook graph api via a request, will fail if the app has been unauthorized by the user. So what I did was to check that before calling the feed dialog method.

Add the following line where you need to test if the app is still authorized:

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

This will just call the method that returns the basic information from the user. If everything is fine, the following method will be called from the delegate:

- (void)request:(FBRequest *)request didLoad:(id)result
{
   //Everything is ok. You can call the dialog method. It should work.
}

If the app has been unauthorized by the user, the following method from the delegate will be called:

- (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.
        }
    }
}

So, as I said before, not the best way, but gets the job done. Hopefully, Facebook will add a method to check for this specific scenario, or add a new method to the delegate that deals with the unauthorized app issue.

这篇关于FaceBook iOS - 检查我的Facebook应用程序是否已被授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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