Wednesday, 16 April 2014

Tag friends in image post/upload to Facebook





Tag friend to Facebook photo
Sharing on Facebook is common features provided by apps developed for IOS. This feature lets app user's to share pictures or status to their Facebook wall and  also allow app developers a platform for their app marketing as under the post its their app name written. So this means sharing to Facebook can triggers more app downloads.In this post i am going to tell you that how can we tag a Facebook friend  to our image upload so that the same post appears on his wall too.
                               

Tagging your friend to an image need two requirements to be full-filled before we initiates the request to upload and tag

1) Facebook Friend ID
2) publish_actions permission fro the logged in user
3) photo id, this the id that will be returned by Facebook when we successfully uploaded our image

Lets jump into the code, if you did not know how to post/upload your photo to Facebook from your app then you can learn it from this link http://www.amaniphoneblog.com/2014/03/iphone-development-post-image-to-your.html

Now as we successfully uploaded our image to Facebook(as told in earlier post or you can check above link for how to upload image to Facebook timeline), its time to tag our Facebook friend to it and allow that post on his/her wall.

To get Facebook friend id,  you have to get their list from Facebook first.
Note: Check this link for getting friends list from Facebook www.amaniphoneblog.com/2014/03/iphone-development-fetch-friend-list.html

Now we have our Facebook friend id(get it from friend list) and photo id(will get this after uploading image successfully, this will come as key name id in result dictionary ) with us, we will tag our friend to the photo id or photo/image uploaded by us.We will now create parameters dictionary which contains list of friends to be tagged.

 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"[{'tag_uid ': '100001985845348'}]", @"tags",nil
                                     ];


 We will get post id/ photo id from our result dictionary returned to us by Facebook on successful image upload/post.

 NSString *photoId = [result objectForKey:@"id"];

 Now, we will tag friend by using startWithGraphPath method of FBRequestConnection class.

 [FBRequestConnection startWithGraphPath:strFormat
                                          parameters:params
                                          HTTPMethod:@"POST"
                                   completionHandler:^(
                                                       FBRequestConnection *connection,
                                                       id result,
                                                       NSError *error
                                                       ) {
                                       if (!error)
                                       {
                                            //success
                                       }
                                       else
                                       {
                                           //error                                   

                                        }
     }];



Below is the complete code that shows how can we tag friend to an image/photo uploaded by us

 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"[{'tag_uid ': '100001985845348'}]", @"tags",nil
                                     ];

 NSString *photoId = [result objectForKey:@"id"];
  NSString *strFormat = [NSString stringWithFormat:@"/%@/tags",photoId];
 [FBRequestConnection startWithGraphPath:strFormat
                                          parameters:params
                                          HTTPMethod:@"POST"
                                   completionHandler:^(
                                                       FBRequestConnection *connection,
                                                       id result,
                                                       NSError *error
                                                       ) {
                                       if (!error)
                                       {
                                            //success
                                       }
                                       else
                                       {
                                           //error

                                       }
    }];


Note: For more info check this https://developers.facebook.com/docs/graph-api/reference/photo/tags/

No comments:
Write comments