Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS FacebooSDK 3.0 FBLoginVIew in modal viewController

I have modal view controller displayed on rightBarButtonItem click. I'm using FbLoginView in this controller as in sample ios-Facebook SDK 3.0 Error 5 When Posting Status Update.

But i'm unable to show modal view controller more than one time. I tried to release FBLoginView on ViewDidUnload but it always crashes on second atempt to open modal view controller.

like image 655
Lizard Avatar asked Aug 15 '12 20:08

Lizard


3 Answers

The FB SDK doesn't seem to like you creating more than one FBLoginView. Maybe you can if you properly terminate the session, but I found it easier just to create the LoginView once and keep it around.

I did this as follows:

1) in my .m modal view controller file, I created a static variable

static FBLoginView* loginView;

2) When loading the modal view controller in my viewDidLoad, instead of

FBLoginView *loginview = [[FBLoginView alloc] initWithPermissions:
                          [NSArray arrayWithObject:@"status_update"]];
loginview.frame = CGRectOffset(loginview.frame, 10, 10);

I added a check to find if its already initialized, like this:

if (!loginView) {
    loginView = [[FBLoginView alloc] initWithPermissions:
                              [NSArray arrayWithObject:@"status_update"]];
    loginView.frame = CGRectOffset(loginView.frame, 10, 10);

}

Beyond that, I just followed the example of FB's HelloFacebook project.

Not pretty code, but it seems to work.

like image 31
Brian Shriver Avatar answered Nov 14 '22 10:11

Brian Shriver


Got the same problem and deal with it for couple days already. And finally this is my solution:

if (!FBSession.activeSession.isOpen) {
    theLoginView = [[FBLoginView alloc] init];
    theLoginView.frame = CGRectOffset(theLoginView.frame,
                                      ([[UIScreen mainScreen] bounds].size.width-theLoginView.frame.size.width)/2,
                                      ([[UIScreen mainScreen] bounds].size.height-theLoginView.frame.size.height)/2 -50);
    theLoginView.delegate = self;
    [self.view addSubview:theLoginView];
    [theLoginView sizeToFit];
}  
//Only close the session when application is terminating, this will save the token information:
- (void)applicationWillTerminate:(UIApplication *)application {
    [FBSession.activeSession close];
}  

//And keep the FBSession within the app until the user want to logout:
[FBSession.activeSession closeAndClearTokenInformation];

Right now for me its working completely fine. Hope this help.

like image 91
EES Avatar answered Nov 14 '22 10:11

EES


I had the same problem. Try to add something like this:

if(!yourFBLoginView)
{
    yourFBLoginView = [FBLoginView alloc] init...];
}

And/or do not forget to close your active session when you dismissing your modalViewController.

if ([[FBSession activeSession] isOpen])
{
    [[FBSession activeSession] close];
} 
like image 37
Hantok Avatar answered Nov 14 '22 11:11

Hantok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!