As described in the README for facebook-ios-sdk, my app calls Facebook#authorize:delegate: before performing any API calls.
This method requires the user to authenticate (either in the Facebook app or in Safari) and then drops control back to my iPhone app. The problem is it asks the user to authenticate every time I call the method. If they’ve already granted permission to my app, they get a message saying that the app is already authorized and they have to press Okay to go back to my app. It doesn’t look very professional.
So I have two questions:
-
Does the user always have to reauthorize in order to make Facebook calls? I always thought it would save the access token somewhere, maybe in the user defaults, so that you wouldn’t need to reauthorize.
-
If the user doesn’t have to reauthorize every time, is there a way to check if my app already has permission, so the user doesn’t have to see that message and press Okay?
It’s unfortunate that the Facebook SDK doesn’t automatically handle it for us. Aside from building it into the SDK yourself, the easiest way is as such:
Each time you allocate _facebook:
If you’ve never saved anything in the NSUserDefaults, then nil values will be set. Nothing’s happened. Otherwise, true values will be set, and
[_facebook isSessionValid];should return TRUE, indicating good values. Go ahead and make Facebook SDK calls as if they are logged in (which they are). If false, then callas usual.
Then make sure to support the following Facebook delegate method:
I use this code in my app, and it works perfectly. I did this from memory, so I apologize if it doesn’t work on copy-and-paste. There might be a mistype somewhere, plus improper memory management. Don’t forget that access tokens expire unless you get offline permission. In any case the code above handles everything.