iPhone Development: Post image to your Facebook timeline in IOS

Posting image to a social network is a common feature provided by every smartphone app Ir-relevant of platform for which it is. In IOS we can also post images to our Facebook timeline using native Faceboook SDK for IOS. In this post i am going to tell you that how we can achieve our desired result i.e posting an image to our timeline via IOS app
First we create our app on Facebook and follow some of its instruction to integrate it with our iPhone app.You can check out all configuration steps from below post
Create app on devlelopers.facebook.com

If you are done with all the settings needed for Facebook integration then its time to know how can we post image to Facebook from our IOS/iPhone app
First we check for active session by using isOpen method of FBSession class.If it’s not then we will open with  openActiveSessionWithPublishPermissions method so that the logged-in user can get publish permission to post picture to his/her wall/timeline.If error occured the show user an alert for error otherwise post image to Facebook and if Facebook session is already opened then obviously post image to Facebook.

if (![[FBSession activeSession]isOpen])
 {
  [FBSession openActiveSessionWithPublishPermissions:@[@”publish_stream”,@”publish_actions”] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
{
                    if (!error)
                    {
                        [FBSession setActiveSession:session];
                        [self postDataToFB:imageData andString:statusPostString];
                    }
                   else
                   {
                      //show error alert
                   }
               
 }];
 }
 else
 {
        [self postDataToFB:imageData andString:statusPostString];
 }

Now at this point we have session for our logged-in user and its time to post.upload picture to Facebook. For this, we need to create a dictionary which takes two parameter’s
1. message: shown with our image
2. picture:- here we pass our image as NSData.
Note:- You can convert image into NSData using below code line
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@”Icon-72.png”]);

Now you also have, image as data to be posted to Facebook wall/timeline.For posting image we will use startWithGraphPath method of  FBRequestConnection class. In this method we will pass our dictionary created above and image is a posted using POST method.In its completion handler block it will return id of the post if successful otherwise an error. Below is the code that post image to Facebook.

-(void)postDataToFB:(NSData *)imageData andString:(NSString *)statusPostString
{
    NSDictionary *parameters = @{@”message”: @”My first picture upload from iPhone app”,@”picture”: imageData};
    [FBRequestConnection startWithGraphPath:@”me/photos”
                                 parameters:parameters
                                 HTTPMethod:@”POST”
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error)
     {
        
         if (error) {
             //show error alert
         } else {
             //show success alert
         }
     }];
}