SLComposeViewController共享教程 [英] Tutorial for SLComposeViewController sharing

查看:141
本文介绍了SLComposeViewController共享教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What are the steps I need to follow to use iOS 6's new SLComposeViewController to post to Facebook, Twitter or Sina Weibo?

推荐答案

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

For details on this framework please see Apple's Social Framework Class Reference

其他教程:


  1. http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html

  2. http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios /

  3. http://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html

  4. https:// developer。 apple.com/videos/wwdc/2012/?id=306

  1. http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
  2. http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
  3. http://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html
  4. https://developer.apple.com/videos/wwdc/2012/?id=306

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

For this example, we will be using the SLComposeViewController's SLServiceTypeFacebook. If you wish to use Twitter or SinaWeibo just change out the SLServiceType to one of the following:


  • SLServiceTypeFacebook

  • SLServiceTypeSinaWeibo

  • SLServiceTypeTwitter

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

iOS 6 has made it very easy to post directly to Facebook, Twitter or Sina Weibo using the SLComposeViewController. This works very similarly to iOS 5's TWTweetComposeViewController.

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

First, in your view controller's header file (.h) #import the Social Framework and the Accounts Framework.

#import< Social / Social .h>

#import< Accounts / Accounts.h>

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

Here we will declare a simple UIButton and an IBAction that we will later link to that button and a void (sharingStatus) which will be used to check that the selected sharing service is available.

@interface ViewController : UIViewController

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

@implementation ViewController

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

Then, in your implementation file (.m), we'll start by implementing the (sharingStatus) void that we declared in the header file. sharingStatus uses SLComposeViewController's isAvailableForServiceType BOOL to return whether or not you can post to the service specified in its argument. In this case, we will use the service type SLServiceTypeFacebook. If the service is available the post button will be enabled with an alpha value of 1.0f, and if the service isn't available the button will be disabled its alpha value set to 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作曲家表格,一个图像和一个链接。这个动作还利用一个完成处理程序来完成作曲家取消和完成的结果。

Here we will set up the IBAction that will call up the composer. For good practice, we will check isAvailableForServiceType again to avoid calling up the composer for a service type that isn't available. (Incase something went wrong during the last check, or if availability somehow changed in the fraction of a second in between tapping the post button and the composers all/init. The code below has been set up to display a Facebook composers sheet with text, an image, and a link. This action also utilises a completion handler for the composer's cancelled and done results.

- (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 中移除此观察者。

In viewWillAppear we will register an observer to ACAccountStoreDidChangeNotification so the application can be notified when account information changes. This observer will then be removed in 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按钮。然后在连接检查器中链接我们之前创建的 IBOutlet IBAction ,就是这样!你已经完成了!

And finally, open up interface builder and add a UIButton which will be the post button. Then in the connections inspector link the IBOutlet and IBAction we created earlier to the button, and that's it! You're done!

这篇关于SLComposeViewController共享教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆