SLComposeViewController完成处理程序

19

您好,如果使用SLComposeViewController CompletionHandler完成推文,我该如何收到通知?以下是发送推文的代码:

  if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Tweeting from my own app! :)"];
        [tweetSheet addURL:[NSURL URLWithString:@"www.someurl.com"]];

        [self presentViewController:tweetSheet animated:YES completion:NULL];
    }
1个回答

49

找到了答案

- (void)showTweetSheet
{
  //  Create an instance of the Tweet Sheet
  SLComposeViewController *tweetSheet = [SLComposeViewController
                                         composeViewControllerForServiceType:
                                         SLServiceTypeTwitter];

  // Sets the completion handler.  Note that we don't know which thread the
  // block will be called on, so we need to ensure that any UI updates occur
  // on the main queue
  tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
    switch(result) {
        //  This means the user cancelled without sending the Tweet
      case SLComposeViewControllerResultCancelled:
        break;
        //  This means the user hit 'Send'
      case SLComposeViewControllerResultDone:
        break;
    }

    //  dismiss the Tweet Sheet
    dispatch_async(dispatch_get_main_queue(), ^{
      [self dismissViewControllerAnimated:NO completion:^{
        NSLog(@"Tweet Sheet has been dismissed.");
      }];
    });
  };

  //  Set the initial body of the Tweet
  [tweetSheet setInitialText:@"just setting up my twttr"];

  //  Adds an image to the Tweet.  For demo purposes, assume we have an
  //  image named 'larry.png' that we wish to attach
  if (![tweetSheet addImage:[UIImage imageNamed:@"larry.png"]]) {
    NSLog(@"Unable to add the image!");
  }

  //  Add an URL to the Tweet.  You can add multiple URLs.
  if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
    NSLog(@"Unable to add the URL!");
  }

  //  Presents the Tweet Sheet to the user
  [self presentViewController:tweetSheet animated:NO completion:^{
    NSLog(@"Tweet sheet has been presented.");
  }];
}

解决了我的目的。 - Itesh
8
提醒一下!在iOS7之后,我们的完成处理程序中不应该关闭SLComposeViewController。 - Sathe_Nagaraja
1
我们如何知道通过Facebook分享是否成功? - lenhhoxung
@lenhhoxung 检查 result 是否等于 SLComposeViewControllerResultDone - JAL
非常有帮助!谢谢! - EmphaticArmPump
刚刚注意到SLComposeViewControllerResult始终会向用户返回“完成”(无论用户点击发送或取消)。 - Sruit A.Suk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接