8

I want to post some text to a users wall using the facebook sdk in an iOS app.

Is posting an open graph story now the only way to do that?

I've found with open graph stories they are really strange, you can only post things in the format "user x a y" where you preset x and y directly on facebook, like user ata a pizza or user played a game. Setting up each one is pretty laborious too because you have to create a .php object on an external server for each one.

Am I missing something or is there a simpler way to go about this?

Tiddly
  • 1,620
  • 3
  • 21
  • 43
  • what sort of text u wanna post? – vishy Apr 03 '13 at 09:24
  • I'm writing an interface for facebook in a game, most posts could probably get by with user scored x points or something along those lines, but I would like the interface to be robust enough to be able to post anything if it's needed. – Tiddly Apr 03 '13 at 09:28

3 Answers3

10

Figured it out by browsing the facebook tutorials a bit more.

-(void) postWithText: (NSString*) message
           ImageName: (NSString*) image
                 URL: (NSString*) url
             Caption: (NSString*) caption
                Name: (NSString*) name
      andDescription: (NSString*) description
{

    NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                   url, @"link",
                                   name, @"name",
                                   caption, @"caption",
                                   description, @"description",
                                   message, @"message",
                                   UIImagePNGRepresentation([UIImage imageNamed: image]), @"picture",
                                   nil];

    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
    {
        // No permissions found in session, ask for it
        [FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:@"publish_actions"]
                                              defaultAudience: FBSessionDefaultAudienceFriends
                                            completionHandler: ^(FBSession *session, NSError *error)
        {
             if (!error)
             {
                 // If permissions granted and not already posting then publish the story
                 if (!m_postingInProgress)
                 {
                     [self postToWall: params];
                 }
             }
         }];
    }
    else
    {
        // If permissions present and not already posting then publish the story
        if (!m_postingInProgress)
        {
            [self postToWall: params];
        }
    }
}

-(void) postToWall: (NSMutableDictionary*) params
{
    m_postingInProgress = YES; //for not allowing multiple hits

    [FBRequestConnection startWithGraphPath:@"me/feed"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error)
     {
         if (error)
         {
             //showing an alert for failure
             UIAlertView *alertView = [[UIAlertView alloc]
                                       initWithTitle:@"Post Failed"
                                       message:error.localizedDescription
                                       delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
             [alertView show];
         }
         m_postingInProgress = NO;
     }];
}
Tiddly
  • 1,620
  • 3
  • 21
  • 43
  • Hello Tiddly, here what is m_postingInProgress. Will you elaborate this & i want to send a message to friends wall..... – Gurpreet Nov 13 '13 at 05:29
  • It was just a simple tag to determine if a post is already being posted, to prevent multi posts and conflicts. – Tiddly Nov 19 '13 at 15:44
  • I just want to send the message to other user through Facebook not post to his wall. But now i am posting to friend's wall which is not correct according to my requirement. – Gurpreet Nov 20 '13 at 06:06
  • I'm not sure how to help you. You should post a new question as your issue is different to mine. If you post a link to the question here in the comments I'll take a look and see if I can help. You will need to post some of your source code in the question for people to be able to help you. – Tiddly Nov 20 '13 at 11:05
  • Just a note. You can more simply use **startForPostWithGraphPath**, or indeed if it is just a photo, simply use **startForUploadPhoto**. – Fattie Dec 16 '13 at 12:16
4

the easiest way of sharing something from your iOS app is using the UIActivityViewController class, here you can find the documentation of the class and here a good example of use. It is as simple as:

NSString *textToShare = @”I just shared this from my App”;
UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.bronron.com"];
NSArray *activityItems = @[textToShare, imageToShare, urlToShare];

UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:TRUE completion:nil];

This will only work on iOS 6 and it makes use of the Facebook account configured in the user settings, and the Facebook SDK is not needed.

neowinston
  • 7,584
  • 10
  • 52
  • 83
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
  • This seemed like exactly what I wanted, but then I spotted it's only for iOS 6 and up :( I need to target iOS 5 as a minimum. Thank you tho. – Tiddly Apr 03 '13 at 09:30
  • I was just adding this last comment to my answer ;) – tkanzakic Apr 03 '13 at 09:31
  • looking into an old project I found that I use this project to post to facebook, it is iOS4 compatible, take a look to it, maybe this is what you need https://github.com/sakrist/FacebookSample – tkanzakic Apr 03 '13 at 15:04
4

You can use Graph API as well.

After all the basic steps to create facebook app with iOS, you can start to enjoy the functionality of Graph API. The code below will post "hello world!" on your wall:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

...

//to get the permission 
//https://developers.facebook.com/docs/facebook-login/ios/permissions  
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
            NSLog(@"publish_actions is already granted.");
        } else {
            FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
            [loginManager logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                //TODO: process error or result.
            }];
        }

    if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
        [[[FBSDKGraphRequest alloc]
          initWithGraphPath:@"me/feed"
          parameters: @{ @"message" : @"hello world!"}
          HTTPMethod:@"POST"]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"Post id:%@", result[@"id"]);
             }
         }];
    }
...

The basic staff is presented here: https://developers.facebook.com/docs/ios/graph

The explorer to play around is here: https://developers.facebook.com/tools/explorer

A good intro about it: https://www.youtube.com/watch?v=WteK95AppF4

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
  • I can't seem to find documentation about what the possible entries are for `parameters`. (For example: can you post an image? A URL? etc. If so, what are the keys? Values?) Thanks! – Olie Nov 07 '15 at 00:24
  • Here you can find the possible entries for parameters: https://developers.facebook.com/docs/graph-api/reference – Darius Miliauskas Nov 09 '15 at 07:25