Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FaceBook iOS - check if my facebook app is allready authorized

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.

like image 381
Garry.S Avatar asked Mar 29 '11 14:03

Garry.S


People also ask

How do I see which Apps are authorized on Facebook?

Tap in the top right of Facebook. Scroll down and tap Settings. Go to the Permissions section and tap Apps and Websites.

How do I remove an authorized app from Facebook?

Scroll down, tap Settings, then tap Apps and Websites. Tap Logged in with Facebook. Tap the app or game that you want to remove. Below the name of the app or game, tap Remove.

How do I know if Facebook SDK is installed?

First, go to your Analytics for Apps page: Analytics for Apps. Then, click "Mobile App Installs” from the left-hand navigation, and check your last install reported to confirm proper installation of the SDK.


1 Answers

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.

like image 94
carlos Avatar answered Sep 19 '22 11:09

carlos