Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook sdk post on wall on iPhone app

I have a problem with implementing Facebook posting on wall in my iPhone application. I installed SDK and linked framework login is working fine. here's the code:

-(IBAction)loginButtonPressed:(id)sender
{
    NSLog(@"loginButtonPressed: called");

    AppDelegate *appdel=[[UIApplication sharedApplication] delegate];
    appdel.facebookSession=[[FBSession alloc] init];
    [appdel.facebookSession openWithCompletionHandler:^(FBSession *session, 
                                                     FBSessionState status, 
                                                     NSError *error)
    {
        //
    }];
}

But I have a problem with posting message on user's wall. Here's the code:

-(IBAction)likeButtonPressed:(id)sender
{
    NSLog(@"likeButtonPressed: called");
    // Post a status update to the user's feedm via the Graph API, and display an alert view 
    // with the results or an error.

    NSString *message = @"test message";
    NSDictionary *params = [NSDictionary dictionaryWithObject:message forKey:@"message"];

    // use the "startWith" helper static on FBRequest to both create and start a request, with
    // a specified completion handler.
    [FBRequest startWithGraphPath:@"me/feed"
                       parameters:params
                       HTTPMethod:@"POST"
                completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                    [self showAlert:message result:result error:error];
                }];

}

Help me please. What's wrong with my code? Or should I add some permissions to login request?

like image 826
user1385666 Avatar asked Jul 18 '12 11:07

user1385666


People also ask

How do I post on Facebook from my Iphone app?

Tap. in the top right of Facebook. Search for the Page you'd like to post on, then select it from the dropdown menu. Tap Posts, then tap Write something on the Page.

What is Facebook SDK iOS?

The Facebook SDK is what allows mobile app developers to integrate Facebook within a mobile app. SDK stands for software development kit, and it allows for a website or app to integrate with Facebook seamlessly. Examples of what you can do with Facebook SDK include: Facebook Login functionality.

What is the latest version of Facebook SDK for iOS?

The current version of the Facebook SDK for iOS is version 14.1. 0. Code and samples for the Facebook SDK for iOS are available on GitHub.


1 Answers

this code worked for me. First we must

#import <FBiOSSDK/FacebookSDK.h>

then

@property (strong, nonatomic) FBRequestConnection *requestConnection;

and of course do not forget to synthesize:

@synthesize requestConnection;

the code itself:

-(IBAction)likeButtonPressed:(id)sender
{
    NSLog(@"likeButtonPressed: called");
    // FBSample logic
    // Check to see whether we have already opened a session.
    if (FBSession.activeSession.isOpen)
    {
        // login is integrated with the send button -- so if open, we send
        [self postOnWall];
    }
    else
    {
        [FBSession sessionOpenWithPermissions:[NSArray arrayWithObjects:@"publish_stream", nil]
                                completionHandler:
             ^(FBSession *session, 
               FBSessionState status, 
               NSError *error)
            {
                 // if login fails for any reason, we alert
                 if (error)
                 {
                     NSLog(@"    login failed");
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                     message:error.localizedDescription
                                                                    delegate:nil
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil];
                     [alert show];
                     // if otherwise we check to see if the session is open, an alternative to
                     // to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen
                     // property of the session object; the macros are useful, however, for more
                     // detailed state checking for FBSession objects
                 }
                 else if (FB_ISSESSIONOPENWITHSTATE(status))
                 {
                     NSLog(@"    sending post on wall request...");
                     // send our requests if we successfully logged in
                     [self postOnWall]; 
                 }
             }];
    };
}

- (void)postOnWall
{
    NSNumber *testMessageIndex=[[NSNumber alloc] init];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"testMessageIndex"]==nil)
    {
        testMessageIndex=[NSNumber numberWithInt:100];
    }
    else
    {
        testMessageIndex=[[NSUserDefaults standardUserDefaults] objectForKey:@"testMessageIndex"];
    };
    testMessageIndex=[NSNumber numberWithInt:[testMessageIndex intValue]+1];
    [[NSUserDefaults standardUserDefaults] setObject:testMessageIndex forKey:@"testMessageIndex"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    // create the connection object
    FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];

    // create a handler block to handle the results of the request for fbid's profile
    FBRequestHandler handler =
    ^(FBRequestConnection *connection, id result, NSError *error) {
        // output the results of the request
        [self requestCompleted:connection forFbID:@"me" result:result error:error];
    };

    // create the request object, using the fbid as the graph path
    // as an alternative the request* static methods of the FBRequest class could
    // be used to fetch common requests, such as /me and /me/friends
    NSString *messageString=[NSString stringWithFormat:@"wk test message %i", [testMessageIndex intValue]];
    FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@"me/feed" parameters:[NSDictionary dictionaryWithObject:messageString forKey:@"message"] HTTPMethod:@"POST"];

    // add the request to the connection object, if more than one request is added
    // the connection object will compose the requests as a batch request; whether or
    // not the request is a batch or a singleton, the handler behavior is the same,
    // allowing the application to be dynamic in regards to whether a single or multiple
    // requests are occuring
    [newConnection addRequest:request completionHandler:handler];

    // if there's an outstanding connection, just cancel
    [self.requestConnection cancel];

    // keep track of our connection, and start it
    self.requestConnection = newConnection;    
    [newConnection start];
}

// FBSample logic
// Report any results.  Invoked once for each request we make.
- (void)requestCompleted:(FBRequestConnection *)connection
                 forFbID:fbID
                  result:(id)result
                   error:(NSError *)error
{
    NSLog(@"request completed");

    // not the completion we were looking for...
    if (self.requestConnection &&
        connection != self.requestConnection)
    {
        NSLog(@"    not the completion we are looking for");
        return;
    }

    // clean this up, for posterity
    self.requestConnection = nil;

    if (error)
    {
        NSLog(@"    error");
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        // error contains details about why the request failed
        [alert show];
    }
    else
    {
        NSLog(@"   ok");        
    };
}
like image 189
user1385666 Avatar answered Sep 28 '22 18:09

user1385666