Mush
Mush

Reputation: 185

Facebook 3.1 SDK with iOS 4.3


I am trying to get Facebook 3.1 SDK working with iOS 4.3+ and display a dialog after the application returns from authentication, However I am having trouble authenticating and displaying the dialog which use to work in the older version of the Facebook SDK.
I have read through the Facebook documentation but some it is not very clear and some things are depreciated while other don't work. Any help in the right direction will be appreciated.

Thanks

Upvotes: 0

Views: 716

Answers (2)

mano
mano

Reputation: 184

1.Install latest facebook sdk in your system and sdk framework in your app.

2.add following code in your app delegate.m

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            @"user_likes",
                            nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}
- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}

3.add following code in your app delegate.h

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

4.last call this delegate when you need

navAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSessionWithAllowLoginUI:YES]; 

Upvotes: 1

Paramasivan Samuttiram
Paramasivan Samuttiram

Reputation: 3738

If you use latest SDK, can you please try the following code.

[FBSession openActiveSessionWithReadPermissions:[NSArray arrayWithObjects:@"read_stream", nil] allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                                  if (session.isOpen) {
                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"You logged in" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                                      [alertView show];
                                  }
                                  else if(status == FBSessionStateClosedLoginFailed) {
                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Loggin failed" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                                      [alertView show];
                                  }
                              }];

Upvotes: 2

Related Questions