i want to upload Multiple images to Facebook from iphone. And i have search so many things and made a code as below: but it's not working. Any help will be appreciated.
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *jsonRequest1 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 1\", \"attached_files\": \"file1\" }";
NSString *jsonRequest2 = #"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 2\", \"attached_files\": \"file2\" }";
NSString *jsonRequestsArray = [NSString stringWithFormat:#"[ %#, %# ]", jsonRequest1, jsonRequest2];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonRequestsArray,#"batch",nil];
NSString *url1=#"http://uhallnyu.files.wordpress.com/2011/11/green-apple.jpg";
NSString *url2=#"http://scm-l3.technorati.com/12/04/24/67277/apple.gif?t=20120424140104";
NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:url1]];
UIImage *img1 = [[UIImage alloc] initWithData:data1];
NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:url2]];
UIImage *img2 = [[UIImage alloc] initWithData:data2];
[params setObject:img1 forKey:#"file1"];
[params setObject:img2 forKey:#"file2"];
[[delegate facebook] requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
You can upload photos to facebook using :-
You can have a switch case to read the type of image from your image file and get imagedata using functions UIImagePNGRepresentation,UIImageJPEGRepresentation,etc.
NSData *yourImageData= UIImagePNGRepresentation(yourImage);
Initialize the dictionary :-
NSMutableDictionary *mutableDictWithParam= [NSMutableDictionary dictionaryWithObjectsAndKeys:#"Your text", #"message", yourImageWithData, #"theSource", nil];
finally send post :-
[facebook requestWithGraphPath:#"/me/photos" andParams:mutableDictWithParam andHttpMethod:#"POST" andDelegate:self];
You can have it in loop to send more than one image.
Related
I want to share image,link etc to Facebook from my iphone app.My app Link,cation,name and description posted successfully. But i can't share image. Please follow my code..
UIImage *image = [UIImage imageNamed:#"sample.png"];
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
self.dictionary =[[NSMutableDictionary alloc] initWithObjectsAndKeys: #"https://www.google.com/ios", #"link",
imgData, #"data",
#"AppName", #"name",
#"Testing", #"caption",
#"say something about this", #"description",
nil];
my share facebook code is..
[facebook requestWithGraphPath:#"/me/feed" andParams:dictionary andHttpMethod:#"POST" andDelegate:self];
My problem is how to take image from bundle and how to share selected image to Facebook?Please help me..
use like below:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My hat image", #"message", data, #"source", nil];
[facebook requestWithGraphPath:#"/me/photos" andParams:params andHttpMethod:#"POST" andDelegate:self];
In my application i am trying to upload Video on facebook wall using FbConnect my Code looks oky But i Don't know why my Video is not uploaded Beacuse if i use the Same Code method for Uploading image it successfully upload the image on facebook.here is my code which i use for image uploading
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"2" ofType:#"png"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfFile:filePath];
UIImage *img = [[UIImage alloc] initWithData:data];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, #"Picture",
nil];
[_facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
[img release];
And for Video i am trying this Code so For
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"];
NSURL *url = [NSURL URLWithString:filePath];
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mp4",
#"video/quicktime", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
[_facebook requestWithGraphPath:#"me/videos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
The Above code give me no error but it retun some text msg in label (the operation Could not be completed).Which is not in case of image uploading.So can some guide me how to fix it.Thanks
Try these lines:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mp4",
#"video/quicktime", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
[_facebook requestWithGraphPath:#"me/videos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Hope this help you.its working fine for me.Now add these lines.
http://stackoverflow.com/questions/10509984/how-to-upload-mp4-video-on-facebook-using-graph-api-in-objective-c
http://%5B1%5D:%20https://github.com/reallylongaddress/iPhone-Facebook-Graph-API
http://stackoverflow.com/questions/12861615/upload-video-to-facebook-using-facebook-new-sdk-on-ios-6-0
AI am using following code to post image on user's wall on facebook
NSData *imageData =UIImagePNGRepresentation(newPageView.image);
UIImage *picture = [UIImage imageWithData:imageData];
NSLog(#"picture is %#",picture);
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:#"this is a test message: postPictureButtonPressed" forKey:#"message"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"/me/photos" withPostVars:variables];
Above code post image in photos album.how can i change it to post image on wall?
try this code
NSURL *url = [NSURL URLWithString:#"YOur Image url"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[variables setObject:img forKey:#"picture"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"/me/photos" withPostVars:variables];
I see in the internet codes that do upload images but the use "Facebook" that isn't recognized, so which framework do I need to use in iOS?
Do you have a code example?
I found this code:
- (IBAction)uploadPhoto:(id)sender {
NSString *path = #"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:img, #"picture", #"my photo's caption text here.", #"message", nil];
[facebook requestWithMethodName: #"photos.upload" andParams:params andHttpMethod:#"POST" andDelegate:self];
[img release];
}
You'll need to add the Facebook iOS SDK to your project.
All the details you need should be found here
I am posting a photo on Facebook from my iPhone app using the Graph API and ASIFormDataRequest.
Everything works fine (photo is uploaded with the right caption). When I try to add tags, photo is still uploaded but with no tag.
Here is how I prepare the data
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/photos",userID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addFile:filePath forKey:#"file"];
[request setPostValue:caption forKey:#"message"];
[request setPostValue:fbManager._facebook.accessToken forKey:#"access_token"];
[request setAllowCompressedResponse:NO];
[request setTimeOutSeconds:60];
//Add tags
NSMutableArray *tagArray=[[NSMutableArray alloc] init];
for(FacebookTag *aTag in aJob.tags) {
NSNumber *_cordX=[NSNumber numberWithFloat:((aTag.x/aJob.image.size.width)*100.0)];
NSNumber *_cordY=[NSNumber numberWithFloat:((aTag.y/aJob.image.size.height)*100.0)];
NSString *cordX=[_cordX stringValue];
NSString *cordY=[_cordY stringValue];
NSMutableDictionary* tag = [NSMutableDictionary dictionaryWithObjectsAndKeys:
aTag.userId, #"to",
cordX, #"x",
cordY, #"y",
nil];
[tagArray addObject:tag];
}
NSData *tagData = [NSKeyedArchiver archivedDataWithRootObject:tagArray];
NSString* tagString = [[NSString alloc] initWithData:tagData encoding:NSUTF8StringEncoding];
[request setPostValue:tagString forKey:#"tags"];
Try this code
// Upload photo
...
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
newPhoto, #"picture", nil];
[self.facebook requestWithGraphPath:#"me/photos" andParams:params
andHttpMethod:#"POST" andDelegate:self];
...
// Response received. Photo upload successful, so add tags
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([request.params objectForKey:#"picture"]) {
// Get photo ID from result
NSString* photoId = (NSString*)[result objectForKey:#"id"];
NSMutableDictionary* params = nil;
for (int i = 0; i < [tagsList count]; i++) {
// tagsList contains user IDs to tag
NSString* uid = (NSString*)[tagsList objectAtIndex:i];
params = [NSMutableDictionary dictionaryWithObjectsAndKeys:uid, #"to",
nil];
NSString* graphUrl = [NSString stringWithFormat:#"%#/tags", [photoId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[self.facebook requestWithGraphPath:graphUrl
andParams:params andHttpMethod:#"POST" andDelegate:self];
}
}
...
}
It is using FBGraph API