iOS 7 SLComposeViewController:检查是否实际发布了推文?

时间:2014-04-01 17:34:50

标签: ios objective-c twitter social-framework

我目前正在使用SLComposeViewController将用户的分数发布到twitter或facebook(取决于他们点击的按钮)。当他们分享时,他们会获得虚拟货币奖励。我面临的问题是它只会告诉我用户是否点击发送或取消。如何检查推文是否实际发布到Twitter?这将有助于打击用户尝试两次提交相同推文的情况(推特不允许)。

这是我现在的代码:

//Check if user can send tweet
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
     //This means the user cancelled without sending the Tweet
     case SLComposeViewControllerResultCancelled:
          NSLog(@"User Canceled");
          break;

     //This means the user hit 'Send'
     case SLComposeViewControllerResultDone:
     NSLog(@"User Tapped Send");
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

     //Show alert & reward user here

     break;
     }                  
     dispatch_async(dispatch_get_main_queue(), ^{
         [self dismissViewControllerAnimated:NO completion:^{
         NSLog(@"Tweet Sheet has been dismissed.");
         }];
     });
     };
         [tweetSheet setInitialText:[NSString stringWithFormat:@"Just scored %ld %@! I challenge anyone to try & beat it! ", (long)scene.score, suffix]];
         [tweetSheet addURL:[NSURL URLWithString:@"http://appstore.com/appurlhere"]];
         [self presentViewController:tweetSheet animated:YES completion:^{
         NSLog(@"Tweet sheet has been presented.");
     }];
   }

 else {

 //Something went wrong, aka no network connection

 };

如何检查推文是否实际发布(使用社交框架)。

1 个答案:

答案 0 :(得分:2)

这个问题已经有一年了,但我刚刚发现它并且刚刚想出来(仅在几分钟前!)一个在我的项目中有效的答案。在我的项目中,我正在计算发送了多少推文来验证用户是否点击了发送。如果用户取消推文,则计数不会增加。希望这会帮助某人。在SLComposeViewControllerResultDone下调用[self tweetSuccessful]。不确定这是不是"对"方式,但似乎是准确的。

-(void)twitterImageTouched:(UIGestureRecognizer *)gesture
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Test";
        [tweetSheet addImage:[UIImage imageNamed:@"yourImageName"];
        [tweetSheet addURL:[NSURL URLWithString:@"http://yourURL"]];
        [self presentViewController:tweetSheet animated:YES completion:nil];

        tweetSheet.completionHandler = ^(SLComposeViewControllerResult result)
        {
            switch (result)
            {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"tweet cancelled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"tweet completed");

                    [self tweetSuccessful];
                    break;

                default:
                    break;
            }
        };
    }

    NSLog(@"Twitter Button Pressed.");
}


-(void)tweetSuccessful
{
    NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:@"numberOfTweets"];
    [[NSUserDefaults standardUserDefaults] setInteger:i + 1 forKey:@"numberOfTweets"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"%li tweets have been shared", (long)i + 1);
}