0

我正在我的应用程序中实现 facebook 发布。并添加一些代码以在 facebook 帐户上发布一些内容。我的代码如下。

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

这是太多的代码,“因为 ios 6 在设置中包含集成的 facebook。” 但我想在 ios 中发布类似 twitter 集成的帖子。我们该怎么做

4

1 回答 1

1

有两种发布方式。

1) 使用 FBNativeDialog 发布。(包括 FacebookSDK.framework) 2)通过 SLComposeViewController 发布。

你想使用哪一个取决于你。你需要添加三个框架,名为 AdSupport.framework、Accounts.framework 和 Social.framework。要使用第一个,您必须包含 #import "FacebookSDK/FacebookSDK.h" 并且发布代码如下:

     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  */
 }

对于第二个,您需要#import "Social/Social.h",代码如下:

     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];
 }
于 2012-11-17T05:43:27.903 回答