formatting a url to appear as string in a facebook post - ios

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.

Related

How to post status feed on Facebook page as pageadmin in iOS?

I want to post status on my Facebook page. Status should be shown on Facebook as a page status not status from any username or from my name.
I tried below code,
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:#"This is a check for page post message", #"message", nil];
/* make the API call */
[FBRequestConnection startWithGraphPath:#"/1521177224766299/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error){
if (result)
{
NSLog(#"Result %#", result);
}
else
{
NSLog(#"%#", error);
}
}];
a message status was posted done page wall but that status come from my name.
But when I tried to hit same post method with page_id/feed POST Query and with same parameter #"this is my status" #"message" from graph api explorer page, it posted status from page not from me.
Please help.
NSString *accesstoken=[NSString stringWithFormat:#"%#","here add your page access_token"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
accesstoken, #"access_token",
postingString, #"message",
// #"http://www.real-timesports.com", #"link",
nil];
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
graphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
NSLog(#"requestToPost==%#",requestToPost);
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"facebook result >> %#", result);
}];
[requestToPostConnection start];
Here is Complete Answer
NSString *accesstoken = <Facebook_Page_Access_Token>
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
#"Post heading", #"name",
accesstoken, #"access_token",
#"Type post message", #"message",
#"Hyperlink to your post", #"link",
#"Add description", #"description",
#"Image url string", #"picture",
nil];
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
graphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error) {
NSLog(#" %#", result);
}
else
{
NSLog(#"%#", error);
}
}];

IOS Facebook SDK, fetch user info

is it possible, using the Facebook SDK, to get the user info from his user id? An important note is that the user would have approved the application and its permissions before the app make the request.
If it is possible, what is the right way to achieve this? I am trying using this code:
[FBRequestConnection startWithGraphPath:#"/100007046299250"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
}];
But I get the following error message:
Error for request to endpoint '/100007046299250': An open FBSession must be specified for calls to this endpoint.
Thanks
After login authentication, you can get details from active FBSession like below
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:#"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: #"http://graph.facebook.com/%#/picture?type=large", facebookId];
}
}];
}

Liking a facebook status using og_likes iOS functional error

I am trying to implement "Liking a Status" into my application. I am using the Facebook SDK references and have come up with this bit of code :
- (void) likeAStatus : (NSString *) postId {
NSString *post = [NSString stringWithFormat:#"http://facebook.com/%#", postId];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
post, #"object",
nil
];
/* make the API call */
[FBRequestConnection startWithGraphPath:#"/me/og.likes"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
NSString *idFromCreate = [NSString stringWithFormat:#"/%#", [result objectForKey:#"id"]];
/* make the API call */
[FBRequestConnection startWithGraphPath:idFromCreate
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
}];
}];
}
Now when I run this on a post that I haven't liked before (I am simply had coding the postID for now) I get this response in the console : (I was just printing the action ID to make sure it was parsing the response properly)
2014-01-13 15:59:43.766 Unifeed[5336:70b] Result is {
id = 10151765515511916;
}
2014-01-13 15:59:43.767 Unifeed[5336:70b] Error is (null)
2014-01-13 15:59:43.767 Unifeed[5336:70b] The ID is /10151765515511916
2014-01-13 15:59:44.052 Unifeed[5336:70b] Result is {
"FACEBOOK_NON_JSON_RESULT" = true;
}
2014-01-13 15:59:44.053 Unifeed[5336:70b] Error is (null)
This is looking like it works, unfortunately when I go and check my Facebook and look at this specific post it is not liked.... If I check my activity log on Facebook I do not see that I liked this status.
Any ideas as to what is going on or any suggestions to a different method of liking a status update? Also is the "FACEBOOK_NON_JSON_RESULT" = true a good thing?
Thanks much!
I have figured it out - it was a differentiation of the graph API call. Now the code looks like this :
- (void) likeAStatus : (NSString *) postId {
NSString *post = [NSString stringWithFormat:#"%#/likes", postId];
/* make the API call */
[FBRequestConnection startWithGraphPath: post
parameters:nil
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
}];
}
Hope this helps someone out in the future!!
Andy

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

IOS Facebook SDK 3 upload image with message

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/

Resources