Pages

Subscribe:

Friday, September 7, 2012

How to integrate new Facebook SDK in an iOS application (Basic log in functionality)

New Facebook SDK in iOS is a great built-in feature for the iOS apps developer.

Add this Framework header in your AppDelegate.h file (don't forget to add the Facebook framework).
#import "FacebookSDK/FacebookSDK.h" // Please replace the double quote (") with ('<' and '>')

Add this line to your AppDelegate.h file

extern NSString *const FBSessionStateChangedNotification;

Add this method declaration in your AppDelegate.h file

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

Add these codes in your AppDelegate.m file

NSString *const FBSessionStateChangedNotification =
@"com.yourCompany.yourAppName:FBSessionStateChangedNotification";

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    NSLog(@"url: %@",url);
    return [FBSession.activeSession handleOpenURL:url];
}


/*
 * Callback for session changes.
 */
- (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;
    }
   
    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];
   
    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
{
    return [FBSession openActiveSessionWithPermissions:permissions
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState state,
                                                         NSError *error) {
                                         [self sessionStateChanged:session
                                                             state:state
                                                             error:error];
                                     }];
}









// Add the following code inside applicationDidBecomeActive: method


if (FBSession.activeSession.state == FBSessionStateCreatedOpening) {   
        [FBSession.activeSession close]; // so we close our session and start over 
    }

// Now it's time to work on your view controller. Add the following observer inside the viewDidLoad: method of your view controller, where you want to share something:

[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(sessionStateChanged:)
     name:FBSessionStateChangedNotification
     object:nil];

// Here is the call back method for the notification observer
- (void)sessionStateChanged:(NSNotification*)notification {
    if (FBSession.activeSession.isOpen) {
        //[self.authButton setTitle:@"Logout" forState:UIControlStateNormal];
        NSLog(@"State Open");
       
      
        }
        else {
        //[self.authButton setTitle:@"Login" forState:UIControlStateNormal];
        NSLog(@"State Closed");
    }
}


// Here is your facebook button's action:

-(IBAction)facebookButtonPressed
{
    // you need to create the instance for your AppDelegate
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate openSessionWithAllowLoginUI:YES];
}

*** If you want to login into your application with facebook login, you need to create an application at developer.facebook.com/apps

*** Will post more functionality (post image, status, link, etc) later