【问题标题】:How to Post to Facebook on iOS 6 in Objective-C using ACAccountStore class如何使用 ACAccountStore 类在 Objective-C 中的 iOS 6 上发布到 Facebook
【发布时间】:2012-09-12 09:30:44
【问题描述】:

我想知道如何使用 Xcode 4.5 上的新框架在 iOS 6 上向 Facebook 发布状态消息。谢谢! :)

【问题讨论】:

标签: xcode facebook ios6 frameworks posting


【解决方案1】:

发布消息相当简单。这几乎就像 Twitter 框架一样。

首先您必须导入框架:社交和帐户

#import <Social/Social.h>
#import <Accounts/Accounts.h>

在您的 .h 文件中:

SLComposeViewController *mySLComposerSheet;

此代码必须包含在您的 .m 文件中的操作中:

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
    {
      mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
        mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social platform to use it, e.g. facebook or twitter
                [mySLComposerSheet setInitialText:[NSString stringWithFormat:@"Test",mySLComposerSheet.serviceType]]; //the message you want to post
       [mySLComposerSheet addImage:yourimage]; //an image you could post
        //for more instance methods, go here: https://developer.apple.com/documentation/social/slcomposeviewcontroller#//apple_ref/doc/uid/TP40012205
        [self presentViewController:mySLComposerSheet animated:YES completion:nil];
    }
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                output = @"Action Cancelled";
                break;
            case SLComposeViewControllerResultDone:
                output = @"Post Successful";
                break;
            default:
                break;
        } //check if everything worked properly. Give out a message on the state.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];

【讨论】:

  • @Blade thans 。但我想在没有“TWRequest”之类的操作表的情况下分享消息,那么 facebook 中的流程是什么?
  • @Blade Grt 建议。它工作得很好,但是当我背靠背发布相同的消息时,我有一个查询,而不是我收到两个弹出窗口。 1. 发帖成功 2. 发帖无法发送,因为连接到 facebook 失败。我只想要一个弹出窗口,以便用户了解。从这两个弹出窗口中,用户对帖子是否成功感到困惑。如果您有任何想法,请建议我。
  • @Blade 我们可以将“通过 iOS 共享链接”更改为“通过 MyAppName 共享链接”吗?
  • @Blade 谢谢我刚刚使用了这段代码。一次编辑:您真的想分配 mySLComposerSheet 两次(一次使用 alloc/init,然后使用 'composeViewControllerForServiceType:')?换句话说,代码在没有 alloc/init 的情况下也可以工作,你只是立即重新分配,除非我遗漏了什么。
  • 另外:如果您没有在开头包含“if”并且用户没有在他们的设置中登录 FB,iOS 将启动一个徽章,询问他们是否愿意登录 FB——避免未登录时没有响应。
【解决方案2】:
- (IBAction)btn_facebook:(id)sender {

    SLComposeViewController *facebookcomposer =
        [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [facebookcomposer setInitialText:@"This is just a test"];
    [facebookcomposer addURL:[NSURL URLWithString:@"http://www.google.com"]];
    [facebookcomposer addImage:[UIImage imageNamed:@"images.jpg"]];

    [self presentViewController:facebookcomposer animated:YES completion:nil];

    [facebookcomposer setCompletionHandler:^(SLComposeViewControllerResult result)
    {
        switch (result)
        {
            case SLComposeViewControllerResultDone:
                NSLog(@"done");
                break;
            case SLComposeViewControllerResultCancelled:
                NSLog(@"cancelled");
                break;

            default:
            break;
        }

    }];

}

- (IBAction)btn_twitter:(id)sender {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *twitter =
            [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [twitter setInitialText:@"this is just a test"];
        [twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];
        [twitter addImage:[UIImage imageNamed:@"images.jpg"]];
        [self presentViewController:twitter animated:YES completion:nil];
    } else {
        NSLog(@"it is not configured");
    }
}

【讨论】:

    【解决方案3】:

    这就是我在我的应用程序中实际使用它的方式。更流畅,让控制器完成艰苦的工作。

    - (IBAction)postToFacebook:(id)sender {
      if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])  {
            NSLog(@"log output of your choice here");
      }
      // Facebook may not be available but the SLComposeViewController will handle the error for us.
      self.mySLComposerSheet = [[SLComposeViewController alloc] init];
      self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
      [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at SomeWhere:\n %@ \n", [self someURLString]]];
      [self.mySLComposerSheet addImage:self.photos.firstObject]; //an image you could post
    
      [self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
    
      [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
            NSString *output;
            switch (result) {
                  case SLComposeViewControllerResultCancelled:
                        output = @"Action Cancelled";
                        break;
                  case SLComposeViewControllerResultDone:
                        output = @"Post Successfull";
                        break;
                  default:
                        break;
            }
            if (![output isEqualToString:@"Action Cancelled"]) {
                  // Only alert if the post was a success. Or not! Up to you. 
                  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                  [alert show];
            }
      }];
    }
    

    【讨论】:

      【解决方案4】:

      好的,我调整了原始帖子,适用于 iOS 6/7。更改 Facebook 的 ServiceType、警报标题和消息。享受吧!

      - (IBAction)tweetMessage:(id)sender {
      
      
        if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) //check if Facebook Account is linked
        {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to Tweet!" message:@"Please login to Twitter in your device settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
              [alert show];
              return;
        }
        //self.mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
        self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at this Place:\n %@ \n", [self someplace]]];
        [self.mySLComposerSheet addImage:self.photos.firstObject];
      
        [self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
        //}
      
        [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
              NSString *output;
              switch (result) {
                    case SLComposeViewControllerResultCancelled:
                          output = @"Action Cancelled";
                          break;
                    case SLComposeViewControllerResultDone:
                          output = @"Post Successfull";
                          break;
                    default:
                          break;
              } //check if everything worked properly. Give out a message on the state.
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
              [alert show];
        }];
      }
      

      【讨论】:

        【解决方案5】:

        当我只想在帖子成功时收到警报,而当用户取消帖子时什么都没有,我该怎么办?

        不幸的是,这不适用于 Twitter……它不再关闭 TweetSheet。这是我的代码:

         if(NSClassFromString(@"SLComposeViewController") != nil)
         {
        
                mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
                mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social plattform to use it, e.g. facebook or twitter
                [mySLComposerSheet setInitialText:[NSString stringWithFormat:story.title,mySLComposerSheet.serviceType]]; //the message you want to post
                [mySLComposerSheet addURL:[NSURL URLWithString:story.link]];
                //for more instance methodes, go here:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40012205
                [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
                    NSString *output;
                    switch (result) {
                        case SLComposeViewControllerResultCancelled:
                            output = NSLocalizedStringFromTable(@"As it seems you didn't want to post to Twitter", @"ATLocalizable", @"");
                            break;
                        case SLComposeViewControllerResultDone:
                            output = NSLocalizedStringFromTable(@"You successfully posted to Twitter", @"ATLocalizable", @"");
                            break;
                        default:
                            break;
                    } //check if everything worked properly. Give out a message on the state.
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                    [alert show];
                }];
                [self presentViewController:mySLComposerSheet animated:YES completion:nil];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-03
          • 1970-01-01
          • 2015-12-02
          • 1970-01-01
          • 2014-05-19
          • 1970-01-01
          相关资源
          最近更新 更多