When I post photo in faceboook one by one through ios. it creates a photo album,I want to post photo separately, don't create the album or thumbnails. upload single post at a time. I am doing R & D more than two days.
please provide a better solution.
Thanks,
UIImage *postImag = [UIImage ImageWithName:#"Hello.png"];
NSString *message = #"Hello Test Message";
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:0];
NSData *imageData = UIImagePNGRepresentation(postImag);
[params setObject:message forKey:#"message"];
if (imageData ) {
[params setObject:imageData forKey:#"source"];
}
/* make the API call */
[FBRequestConnection startWithGraphPath:#"me/photos" parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
if (error) {
NSLog(#"Error: %#",[error localizedDescription]);
} else {
NSLog(#"Successfully shared.");
}
completionAction();
}];
}
Related
I am using the following code to upload an image from my iOS to Facebook.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"MY TEST MSG..." forKey:#"message"];
[params setObject:UIImagePNGRepresentation(self.editImageView.image) forKey:#"picture"];
[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/photos" parameters:params HTTPMethod:#"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"uploaded");
} else {
NSLog(#"Graph API Error: %#", [error description]);
}
}];
My app got stuck for couple of minutes and show the error message.
Can any one please help me?
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];
UIImage *picture = FilterImage;
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:[NSString stringWithFormat:#"Hello testin"] forKey:#"message"];
[fbGraph doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"Now log into Facebook and look at your profile & photo albums...");
I am trying to create a new album on a user's Facebook account, and upload images to that album. I have succeeded to create an album, but when I start uploading images to it only the last image selected gets uploaded the number of times the images I have selected.
This is my piece of code to create a Facebook album and upload images to it
- (void)createFacebookAlbum {
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setObject:albumNAME forKey:#"name"];
[parameters setObject:#"Test message" forKey:#"message"];
FBRequest* request = [FBRequest requestWithGraphPath:#"me/albums" parameters:parameters HTTPMethod:#"POST"];
NSLog(#"creating facebook album");
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:request
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
albumId = [result objectForKey:#"id"];
NSLog(#"OK %#", albumId);
[self publishContentToAlbum:[result objectForKey:#"id"]];
}
else {
NSLog(#"Error: %#",error.userInfo);
}
}];
[connection start];
}
-(void)uploadBulkPhotosToAlbum:(NSMutableArray *)photoArray albumId:(NSMutableArray *)albumId{
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSString *graphPath = [NSString stringWithFormat:#"%#/photos",albumId];
NSMutableString *jsonFormat = [[NSMutableString alloc] init];
[jsonFormat setString:#"["];
for (int i = 0; i < (NSUInteger *)countere; i++)
{
if (i != 0)
{
[jsonFormat appendString:#","];
}
NSString *fileName = [NSString stringWithFormat:#"file%d",i];
NSString *jsonRequest = [NSString stringWithFormat:#"{ \"method\": \"POST\", \"relative_url\": \"%#\", \"attached_files\": \"%#\" }",graphPath,fileName];
[jsonFormat appendString:[NSString stringWithFormat:#" %#",jsonRequest]];
}
[jsonFormat appendString:#" ]"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonFormat,#"batch",nil];
for (int i = 0; i <(NSUInteger *)countere; i++)
{
NSString *fileName = [NSString stringWithFormat:#"file%d",i];
NSData *data = UIImagePNGRepresentation([photoArray objectAtIndex:i]);
[params setObject:data forKey:fileName];
}
FBRequest *request1 = [FBRequest requestWithGraphPath:graphPath
parameters:params
HTTPMethod:#"POST"];
[connection addRequest:request1
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"Photo uploaded successfuly! %#",result);
} else {
NSLog(#"Photo uploaded failed :( %#",error.userInfo);
}
}];
[connection start];
}
Can anyone help me to solve my problem?
Thanks for the reply.
Is it possible to use the Facebook SDK or Graph API to create a post like the one below?
I essentially want to combine these two together:
[FBRequestConnection startForUploadPhoto:[UIImage imageNamed:#"myImage"] completionHandler:nil];
[FBRequestConnection startForPostStatusUpdate:#"Hello, World!" completionHandler:nil];
Thanks.
Use following method :
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
name:params.name
caption:params.caption
description:params.description
picture:params.picture
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog([NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
// Success
NSLog(#"result %#", results);
}
}];
Here is the reference : https://developers.facebook.com/docs/ios/share
Try this,
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
if (message) {
[dictionary setObject:message forKey:#"message"];
}
FBRequest *request = [FBRequest requestWithGraphPath:#"me/photos" parameters:dictionary HTTPMethod:#"POST"];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[connection start];
Is it possible to create multiple Picture albums dynamically in Facebook through objective C, so that we have text definition for album, every time we upload certain pictures ?
The current code, we are using is as follows :
for (UIView *view in scrollView.subviews)
{
if (view.tag == IMAGE_VIEW_TAG) {
NSData *attachmentData = [self getImageData:view];
UIImage *img = [UIImage imageWithData:attachmentData];
FBRequest *photoUploadRequest = [FBRequest requestForUploadPhoto:img];
[photoUploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
NSLog(#"This is Image");
}
if (view.tag == TEXT_VIEW_TAG)
{
NSString *message = #""
message = [self getTextData:view];
NSLog(#"mesage values %#",message);
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:message, #"message",nil];
[FBRequest startWithGraphPath:#"me/feed" parameters:params HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error){
I am using the following code to upload an image to Facebook wall :
UIImage *img = [UIImage imageNamed:#"logoits.png"];
[FBRequestConnection startForUploadPhoto:img
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Facebook posting %#",result);
NSLog(#"Facebook posting %#",error);
}];
It works as expected but I would like to add a message or title to this uploaded photo, and looking at the documentation of FBRequestConnection there is no example of that. http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequestConnection/
How do I upload an image with message?
In the Facebook SDK 3.0 only a image can be post using the given methods, and for other actions you need to use graph a pi's.
So for post a image with message you need to use graph-api. Here is the line of codes which lets you to post a image with message on users timeline.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"your custom message" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
[self alertWithTitle:#"Facebook" message:#"Unable to share the photo please try later."];
}
else
{
//showing an alert for success
[UIUtils alertWithTitle:#"Facebook" message:#"Shared the photo successfully"];
}
_shareToFbBtn.enabled = YES;
}];
and also make sure _shareToFbBtn is enabled only after login is success.
linesvishy's solution works great and it's also worth to note that if you were using Facebook SDK 3.1 and you used method
FBRequest *request = [FBRequest requestForUploadPhoto:_imageToShare];
You can just use the following lines to replace it:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:_textToShare forKey:#"message"];
[params setObject:_imageToShare forKey:#"picture"];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/photos" parameters:params HTTPMethod:#"POST"];
Hope it helps! Happy Coding!
Here is an example that posts and image and a note:
http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/publish-open-graph-story/