Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach 5 images to a Facebook feed post using Facebook ios sdk 3.0

I'm trying to post to the user's feed something like this (it initially shows only one image but when you click "show more" you see all five images)

Post with 5 images

My code looks like this :

NSMutableArray *properties = [[NSMutableArray alloc] initWithCapacity:5];
NSMutableArray *media = [[NSMutableArray alloc] initWithCapacity:5];
for (MyObject *object in self.myObjects) {
    [properties addObject:[NSDictionary dictionaryWithObjectsAndKeys:object.name,@"text",
                                                                     object.link,@"href", nil]];
    NSString *imageUrlString = object.url.absoluteString;
    [media addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"image",@"type",
                                                                imageUrlString,@"src",
                                                                object.link,@"href", nil]];
}
NSData *propertyData = [NSJSONSerialization dataWithJSONObject:properties
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
NSString *propertiesString = [[NSString alloc] initWithData:propertyData
                                                   encoding:NSUTF8StringEncoding];
NSData *mediaData = [NSJSONSerialization dataWithJSONObject:media
                                                    options:NSJSONWritingPrettyPrinted
                                                      error:nil];
NSString *mediaString = [[NSString alloc] initWithData:mediaData
                                              encoding:NSUTF8StringEncoding];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:myAppID, @"app_id",
                                                                  link, @"link",
                                                                  name, @"name",
                                                                  caption, @"caption",
                                                                  propertiesString, @"properties",
                                                                  mediaString, @"media",
                                                                  description, @"description", nil];
[FBRequestConnection startWithGraphPath:@"me/feed"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:completionHandler];

This only posts one image but I need to post all 5 of them.

EDIT : We are already posting 5 images in one post through janrain engage library so it is doable!

like image 477
Moxy Avatar asked Sep 11 '12 08:09

Moxy


1 Answers

You will need to use the Facebook Connect API directly: the iOS SDK does not expose this kind of functionality.

You should have a look at the Publishing section of the Graph Photo API which suggests this URL to upload an image (don't forget to ask for the publish_stream credential):

POST https://graph.facebook.com/USER_ID/photos

message=[optional description]
source=[the image's data]
place=[optional image's location]

With the iOS Facebook Connect SDK that would give us this call, given you have a Facebook instance called facebook and a UIImage instance called image:

[facebook requestWithMethodName:@"/USER_ID/photos"
                      andParams:[NSDictionary dictionaryWithObjectsAndKeys:
                                 UIImageJPEGRepresentation(image, 0.7), @"source",
                                 @"My puppy is so cute!!!", @"message",
                                 nil]
                  andHttpMethod:@"POST"
                    andDelegate:self];
like image 161
nivritgupta Avatar answered Nov 08 '22 16:11

nivritgupta