How to send text and images using Facebook API in iPhone SDK?

2.6k views Asked by At

In my iPhone app, I want to send text as well as images to facebook.

Currently the facebook API suggest that there are seperate user permissions that enable us to do so.

Is there a way out where in I can do both using only one API in my project?

If Yes, How can we do so?

References to any articles or tutorials would be very helpful

Please Help & Suggest.

Thanks

2

There are 2 answers

0
Sergey Kopanev On

I'm not sure that it is possible. I've post image with next solution:

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               takedPhoto, @"picture", 
                               @"some text", @"message",             
                               nil];


[facebook requestWithGraphPath:@"/me/photos" andParams:params
                 andHttpMethod:@"POST" andDelegate:self];

takedPhoto is UIImage. But it is only loading photos in photo album (and approve it if you allow permissions when logging in). I don't know how can I post message on wall with this uploaded image... Regards,

0
Nishant B On

Yes you can do this.

Please have a look at all the methods as below:

- (IBAction)uploadPhoto:(id)sender {
    [self postOnFacebook];
}

//Post Message and Photo on Facebook Function

-(void)postOnFacebook
{
NSString *strPostMessage = [self createMessageForPostOnFacebook:74.112 withLongitude:21.85];
//UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:objClsProductSearch.strProductImageURL]]];
//UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"]]];
UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/08/HUDTutorial.jpg"]]];

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               strPostMessage,@"message",img, @"source",
                               nil];

[_facebook requestWithGraphPath:@"/me/photos"
                      andParams:params
                  andHttpMethod:@"POST"
                    andDelegate:self];

[img release];
}

//Create Message for posting on Facebook

-(NSString *)createMessageForPostOnFacebook:(double)pfltLatitude withLongitude:(double)pfltLongitude
{
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pfltLatitude, pfltLongitude];
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",@"", @""];
//NSError* error;
//NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
//NSArray *listItems = [locationString componentsSeparatedByString:@","];
NSString *strMessage = nil;
/*if([[listItems objectAtIndex:0] isEqualToString:@"200"]) 
 {
 strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f &  Longitude : %f \n Product Rating : %d",objClsProductSearch.strCategoryname,[locationString substringFromIndex:6],appDelegate.dblLatitude,appDelegate.dblLongitude,btnTmp.tag];
 }*/
strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f &  Longitude : %f \n Product Rating : %d", @"Cat Name", @"Location", 74.112, 21.85, 1];

return strMessage;
}

I hope you will have success using the above code.

Let me know in case of any difficulty.

In you app delegate, have a look at below methods whether it is implemented or not.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    //return [[controller facebook] handleOpenURL:url];
    //return [[objPage2ViewController facebook] handleOpenURL:url];
    return [[[self.navigationController topViewController] facebook] handleOpenURL:url];
}

Cheers