user1396086
user1396086

Reputation:

How can we post something on facebook in ios 6 like twitter?

I am implementing facebook posting in my app. And add some code to post something on facebook account. My code is as follows.

    - (void)publishStory
{
 NSLog(@"publish story called .......");

 [FBRequestConnection
  startWithGraphPath:@"me/feed"
  parameters:self.postParams
  HTTPMethod:@"POST"
  completionHandler:^(FBRequestConnection *connection,
                      id result,
                      NSError *error) {
   NSString *alertText;
   if (error) {
    alertText = [NSString stringWithFormat:
                 @"error: domain = %@, code = %d",
                 error.domain, error.code];
   } else {
    alertText = [NSString stringWithFormat:
                 @"Posted action, id: %@",
                 [result objectForKey:@"id"]];
   }
   // Show the result in an alert
   [[[UIAlertView alloc] initWithTitle:@"Result"
                               message:alertText
                              delegate:self
                     cancelButtonTitle:@"OK!"
                     otherButtonTitles:nil]
    show];
  }];
}

-(IBAction)cancelButtonAction
{
 [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)shareButtonAction
{
 // Add user message parameter if user filled it in
 if (![self.postMessageTextView.text isEqualToString:@""]) {
  [self.postParams setObject:self.postMessageTextView.text
                      forKey:@"message"];
 }

 // Ask for publish_actions permissions in context
 if ([FBSession.activeSession.permissions
      indexOfObject:@"publish_actions"] == NSNotFound) {
  // No permissions found in session, ask for it
   [FBSession.activeSession reauthorizeWithPublishPermissions:
   [NSArray arrayWithObjects:@"publish_actions",@"publish_stream", nil]
   defaultAudience:FBSessionDefaultAudienceFriends
   completionHandler:^(FBSession *session, NSError *error) {
    if (!error) {
     // If permissions granted, publish the story
     NSLog(@"not error");
     [self publishStory];
    }
   }];
 } else {
  // If permissions present, publish the story
  NSLog(@"In else condition");
  [self publishStory];
 }
}

this is too much code for , "as ios 6 contains integrated facebook in settings." But I want to post like twitter integration in ios.How can we do that

Upvotes: 0

Views: 946

Answers (1)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

There are two ways for posting.

1)Post using FBNativeDialog. (inlcude FacebookSDK.framework) 2)Post via SLComposeViewController.

Which one you want to use is up to you.You need to add three frameworks named AdSupport.framework,Accounts.framework and Social.framework. For using first one you have to include #import "FacebookSDK/FacebookSDK.h" and code for posting is as follows:

     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

 BOOL displayedNativeDialog = [FBNativeDialogs presentShareDialogModallyFrom:self initialText:@"" image:[UIImage imageNamed:@"iossdk_logo.png"] url:[NSURL URLWithString:@"https://developers.facebook.com/ios"]
  handler:^(FBNativeDialogResult result, NSError *error)
 {
   if (error) {
    alert.message=@"Fail posting due to some error!";
    [alert show];
    /* handle failure */
   } else {
    if (result == FBNativeDialogResultSucceeded) {
     alert.message=@"Posted Successfully!";
     [alert show];
     /* handle success */
    } else {
     /* handle user cancel */
    }
   }}];

 if (!displayedNativeDialog) {
  /* handle fallback to native dialog  */
 }

For second one you need #import "Social/Social.h" and the code is as follows:

     SLComposeViewController *fbComposer =
 [SLComposeViewController
  composeViewControllerForServiceType:SLServiceTypeFacebook];


 if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
 {
  SLComposeViewControllerCompletionHandler __block completionHandler=
  ^(SLComposeViewControllerResult result){

   [fbComposer dismissViewControllerAnimated:YES completion:nil];

   switch(result){
    case SLComposeViewControllerResultCancelled:
    default:
    {
     NSLog(@"Cancelled.....");

    }
     break;
    case SLComposeViewControllerResultDone:
    {
     NSLog(@"Posted....");
     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sent"
                                                      message:nil
                                                     delegate:nil
                                            cancelButtonTitle:@"Dismiss"
                                            otherButtonTitles: nil];
     [alert show];
    }
     break;
   }};

  [fbComposer addImage:[UIImage imageNamed:@"iossdk_logo.png"]];
  [fbComposer setInitialText:@"The initial text you want to send"];
  [fbComposer addURL:[NSURL URLWithString:@"https://developers.facebook.com/ios"]];
  [fbComposer setCompletionHandler:completionHandler];
  [self presentViewController:fbComposer animated:YES completion:nil];
 }

Upvotes: 1

Related Questions