IOS Facebook SDK 3 upload image with message - ios

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/

Related

share photos on facebook through iOS create Album

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();
}];
}

Ios Can i get Upload progress from Graph api Facebook?

In my app i need to upload a video to the user's facebook wall. I use this code and it works:
[FBRequestConnection startWithGraphPath:#"me/videos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
}
else
{
//showing an alert for success
}
}];
I wanna add a progress bar during the upload time... but i can't find anything that works...I have read the documentation also, but i can't find anything... There is a way manage a progress bar for #"me/videos" ?
You can set a FBRequestConnectionDelegate on the FBRequestConnection, see https://developers.facebook.com/docs/reference/ios/current/protocol/FBRequestConnectionDelegate/, and implement the requestConnection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite method.
Your progress should be totalBytesWritten/totalBytesExpectedToWrite.
You also need to create the FBRequest and the FBRequestConnection separately if you want to set the delegate, something like:
FBRequest *request = [FBRequest requestForUploadVideo:#"some_path"];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:request completionHandler:YOUR_HANDLER_HERE];
connection.delegate = self;
[connection start];

formatting a url to appear as string in a facebook post

I am trying to post a link formatted as a string on facebook. Please see the relevant code below:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
NSString *modifiedTitle = [NSString stringWithFormat:#"<a href= https://www.google.com> My App </a>"];
[params setObject:modifiedTitle forKey:#"message"];
[params setObject:UIImagePNGRepresentation(picture) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
if (error)
{
//showing an alert for failure
NSLog(#"ERROR in posting image to Facebook");
}
else
{
//showing an alert for success
NSLog(#"Result after posting image successfully = %#",result);
}
// button.enabled = YES;
}];
The message appeared as:
<a href= https://www.google.com> My App </a>
instead of
My App
For one thing, ensure you're posting an immmutable dictionary into the startWithGraph... method by using the copy method:
NSDictionary* paramsImmutable = params.copy;
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:paramsImmutable
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
That way you won't have any changes to worry about on that object during the method's lifetime.
Next, is your params including any sort of quotes around the url? I.e.
NSString *modifiedTitle = #"<a href='https://www.google.com'>My App</a>";
That's just a start.

Posting a map to Facebook using Facebook SDK from iOS

I am posting an image to facebook through FBRequestConnection.
How can I post a map to FB by giving a latitude and longitude. (Also attaching some text with this)
Please advise.
if (FBSession.activeSession.isOpen) {
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"Posting from padivattom" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
}
else
{
//showing an alert for success
}
}];
I used the method:
[FBWebDialogs presentFeedDialogModallyWithSession:
parameters:
handler:
to post message and map to facebook. (Its post to news feed).
But you can try to add to your dictionary: [params setObject:[NSString stringWithFormat:#"https://maps.google.com/?q=%#,%#",latitude,longitude] forKey:#"link"];
(If it doesn't work, you can find the Feed Dialog sample on facebook developers page).
I hope it helps

how to comment on particular photo using FBRequestConnection(Facebook Graph API) in ios?

I want to post comment on particular photo...how can i achieve that using FBRequestConnection (Graph API), I also used Graph API Explorer, but i am not getting how to do that.please can any one explain me in brief.?
Thanks
If you use graphAPI you try with this code..
NSString *commentid=[NSString stringWithFormat:#"%#/comments",groupId];
NSMutableDictionary *dictVar=[[NSMutableDictionary alloc]init];
[dictVar setValue:textView.text forKey:#"message"];
FbGraphResponse *response=[fbgraph doGraphPost:commentid withPostVars:dictVar];
NSLog(#"%#",response.htmlResponse);
Hope this helps :)
To post a comment on any {object_id} use the following snippet of code
NSDictionary *param = #{#"message": commentText};
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
FBRequest *request = [FBRequest requestWithGraphPath:[NSString stringWithFormat:#"/%#/comments",mediaId] parameters:param HTTPMethod:#"POST"];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error) {
aCompletion(NO);
}else{
aCompletion(YES);
}
}];
[connection start];
Hope this will be helpful.

Resources