I'm having trouble logging in with publish permissions in the facebook 3.1 ios sdk.
My app has a button to share a video, and when the user clicks it I want to add the basic + publish permission. As I understand, i have to do two calls -
openActiveSessionWithReadPermissions
, and thenreauthorizeWithPublishPermissions
Here's the code I'm using now:
//Opens a Facebook session and optionally shows the login UX.
- (void)openSessionForReadPermissions
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
//this is called even from the reauthorizeWithPublishPermissions
if (state == FBSessionStateOpen && !error)
{
[self openSessionForPublishPermissions];
}
else if (state == FBSessionStateClosedLoginFailed)
{
[FBSession.activeSession closeAndClearTokenInformation];
[[NSNotificationCenter defaultCenter] postNotificationName:FBLoginErrorNotification object:session];
}
}];
}
-(void)openSessionForPublishPermissions
{
NSArray* permissions = [NSArray arrayWithObject:@"publish_stream"];
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:
^(FBSession *session, NSError *error)
{
if (!error)
{
[[NSNotificationCenter defaultCenter]
postNotificationName:FBLoginSuccessNotification
object:session];
}
else
{
[[NSNotificationCenter defaultCenter]
postNotificationName:FBLoginErrorNotification
object:session];
}
}];
}
I see that the block in the openSessionForReadPermissions is called twice (once with FBSessionStateOpen and once with FBSessionStateOpenTokenExtended from the openSessionForPublishPermissions call), and I get a ErrorReauthorizeFailedReasonUserCancelled when first trying to login to the app (if O deleted all app permissions before).
What is the proper way to implement this login? The app does not require Facebook log-in, except for this one feature, so the login process should be on the same button push.
Thanks!
I ran across this same issue. The solution I found was wrapping the call to [self openSessionForPublishPermissions];
in a dispatch_async block.
Example:
dispatch_async(dispatch_get_current_queue(), ^{
[self openSessionForPublishPermissions];
});
The reason is that the call to reauthorize.. needs to be after the event loop of which openActiveSession.. is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With