SLComposeViewController共享教程

时间:2012-09-19 21:37:28

标签: ios ios6 slcomposeviewcontroller social-framework

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

3 个答案:

答案 0 :(得分:141)

有关此框架的详细信息,请参阅Apple的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. https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html
  4. 对于此示例,我们将使用SLComposeViewController的{​​{1}}。如果您希望使用Twitter或SinaWeibo,只需将SLServiceType更改为以下之一:

    • SLServiceTypeFacebook
    • SLServiceTypeSinaWeibo
    • SLServiceTypeTwitter

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

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

    #import

    #import <Social/Social.h>

    在这里,我们将声明一个简单的#import <Accounts/Accounts.h>和一个UIButton,我们稍后将链接到该按钮和IBAction(sharingStatus),它将用于检查所选的共享服务是可用的。

    void

    然后,在您的实现文件(.m)中,我们将首先实现我们在头文件中声明的(sharingStatus)void。 sharingStatus使用@interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton; - (IBAction)facebookPost:(id)sender; - (void)sharingStatus; @end @implementation ViewController 的{​​{1}} BOOL来返回您是否可以发布到其参数中指定的服务。在这种情况下,我们将使用服务类型SLComposeViewController。如果该服务可用,则将启用post值按钮,其值为1.0f,如果该服务不可用,则该按钮将被禁用,其alpha值设置为0.5f。

    isAvailableForServiceType

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

    IBAction

    isAvailableForServiceType中,我们会向- (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这将是post按钮。然后在连接检查器中将我们之前创建的- (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链接到按钮,就是这样!你做完了!

    enter image description here

答案 1 :(得分:25)

只需使用此代码在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。

答案 2 :(得分:13)

  

安全使用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];
        }];
    }