【问题标题】:SLComposeViewController CompletionHandlerSLComposeViewController CompletionHandler
【发布时间】:2012-11-17 06:29:34
【问题描述】:

您好,如果使用 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];
    }

【问题讨论】:

    标签: objective-c ios xcode


    【解决方案1】:

    找到答案

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

    【讨论】:

    • 谢谢。解决了我的目的。
    • 请注意!在 iOS7 之后,我们不能在完成处理程序中关闭 SLComposeViewController。
    • 如何知道通过facebook分享是否成功?
    • @lenhhoxung 检查result 是否等于SLComposeViewControllerResultDone
    • 非常有帮助!谢谢!
    猜你喜欢
    • 2016-04-02
    • 2015-01-10
    • 2017-08-04
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多