it-swarm.dev

SLComposeViewController共享教程

使用iOS 6的新SLComposeViewController发布到Facebook,Twitter或新浪微博需要遵循哪些步骤?

76
Mick MacCallum

有关此框架的详细信息,请参阅Apple的 社会框架类参考

其他教程:

  1. http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
  2. http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
  3. https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html

对于这个例子,我们将使用SLComposeViewControllerSLServiceTypeFacebook。如果您希望使用Twitter或SinaWeibo,只需将SLServiceType更改为以下之一:

  • SLServiceTypeFacebook
  • SLServiceTypeSinaWeibo
  • SLServiceTypeTwitter

iOS 6使用SLComposeViewController直接发布到Facebook,Twitter或新浪微博上非常容易。这与iOS 5的TWTweetComposeViewController非常相似。

首先,在您的视图控制器的头文件(.h)#import社交框架和帐户框架中。

#import <Social/Social.h>

#import <Accounts/Accounts.h>

在这里,我们将声明一个简单的UIButtonIBAction,我们稍后将链接到该按钮和void(sharingStatus),它将用于检查所选的共享服务是否可用。

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton;
- (IBAction)facebookPost:(id)sender;
- (void)sharingStatus;
@end

@implementation ViewController

然后,在您的实现文件(.m)中,我们将首先实现我们在头文件中声明的(sharingStatus)void。 sharingStatus使用SLComposeViewControllerisAvailableForServiceType BOOL来返回是否可以发布到其参数中指定的服务。在这种情况下,我们将使用服务类型SLServiceTypeFacebook。如果该服务可用,则将启用发布按钮,其alpha值为1.0f,如果该服务不可用,则该按钮将被禁用,其alpha值设置为0.5f。

- (void)sharingStatus {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"service available");
        self.easyFacebookButton.enabled = YES;
        self.easyFacebookButton.alpha = 1.0f;
    } else {
        self.easyFacebookButton.enabled = NO;
        self.easyFacebookButton.alpha = 0.5f;
    }
}

在这里,我们将设置IBAction,它将调用作曲家。为了更好的做法,我们将再次检查isAvailableForServiceType以避免为不可用的服务类型调用composer。 (在最后一次检查过程中出现问题,或者如果在点击发布按钮和作曲家all/init之间的某一时间内某种程度上可用性发生了变化。下面的代码已经设置为显示带有文本的Facebook作曲家表格,图像和链接。此动作还利用完成处理程序来完成作曲家取消和完成的结果。

- (IBAction)facebookPost:(id)sender {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [mySLComposerSheet setInitialText:@"iOS 6 Social Framework test!"];

        [mySLComposerSheet addImage:[UIImage imageNamed:@"myImage.png"]];

        [mySLComposerSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing"]];

        [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {

             switch (result) {
                 case SLComposeViewControllerResultCancelled:
                     NSLog(@"Post Canceled");
                     break;
                 case SLComposeViewControllerResultDone:
                     NSLog(@"Post Sucessful");
                     break;

                 default:
                     break;
             }
         }];

        [self presentViewController:mySLComposerSheet animated:YES completion:nil];
    }
}

viewWillAppear中,我们将向ACAccountStoreDidChangeNotification注册一个观察者,以便在帐户信息发生变化时通知应用程序。然后将在viewDidDisappear中删除此观察者。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharingStatus) name:ACAccountStoreDidChangeNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:ACAccountStoreDidChangeNotification];
}

最后,打开界面构建器并添加一个UIButton,它将是post按钮。然后在连接检查器链接我们之前创建的IBOutletIBAction到按钮,就是这样!你完成了!

enter image description here

141
Mick MacCallum

只需使用此代码在Facebook上分享即可。

SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:@"First post from my iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:@"http://www.appcoda.com"]];
[controllerSLC addImage:[UIImage imageNamed:@"test.jpg"]];
[self presentViewController:controllerSLC animated:YES completion:Nil];

如果你想要这个用于Twitter,那么只需更改SLServiceTypeTwitter。

25
Desert Rose

安全使用SLComposeViewController

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *fbPost = [SLComposeViewController
                                               composeViewControllerForServiceType: SLServiceTypeFacebook];
        [fbPost setInitialText:@"Text You want to Share"];
        [fbPost addImage:[UIImage imageNamed:@"shareImage.png"]];
        [self presentViewController:fbPost animated:YES completion:nil];
        [fbPost setCompletionHandler:^(SLComposeViewControllerResult result) {
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"Post Canceled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"Post Sucessful");
                    break;
                default:
                    break;
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
13
A R