我们怎样才能在ios 6上发布一些像twitter这样的东西?

时间:2012-11-17 05:32:27

标签: facebook ios6

我正在我的应用中实现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一样的内容。我们可以这样做吗

1 个答案:

答案 0 :(得分: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];
 }