xcode 如何使用 ACAccountStore 类在 Objective-C 中在 iOS 6 上发布到 Facebook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12505458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:33:30  来源:igfitidea点击:

How to Post to Facebook on iOS 6 in Objective-C using ACAccountStore class

xcodefacebookios6frameworksposting

提问by jaytrixz

I want to know how to post a status message to Facebook on iOS 6 using the new frameworks on Xcode 4.5. Thanks! :)

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

回答by Blade

Posting a message is rather simple. It's almost like with the Twitter Framework.

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

First you have to import the Frameworks: Social and Accounts

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

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

In your .h file:

在您的 .h 文件中:

SLComposeViewController *mySLComposerSheet;

This code has to be included inside your action in your .m file:

此代码必须包含在您的 .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];
    }];

回答by ashvin

- (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");
    }
}

回答by Stephan K?nig

And what do I have to do, when I just want to receive an alert in case the post was successful, and nothing when the user cancelled the post?

当我只想在发布成功时收到警报,而在用户取消发布时什么都不做时,我该怎么办?

And unfortunately this does not work properly for Twitter... It doesn't dismiss the TweetSheet anymore. Here is my code:

不幸的是,这在 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];

回答by FrostyL

Ok guys, so I tweaked the original post, works for iOS 6/7. Change ServiceType, Alert Title and Message for Facebook. Enjoy!

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

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

回答by FrostyL

This is how I actually use it in my app though. Smoother and lets the controller do the hard work.

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

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