0

我一直在做一个程序,负责将图片或文字(或两者)发布到 facebook 和 twitter。但我想同时做这两件事,所以我写了这样的代码:

//POST TO FACEBOOK
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        slcvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [slcvc addImage:bim];
        [slcvc setInitialText:tf.text];
        [self presentViewController:slcvc animated:YES completion:NULL];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook - not logged in!" message:@"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:@"Too bad!" otherButtonTitles:nil];
        [alert show];
    }

//POST TO TWITTER
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        slcvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [slcvc addImage:bim];
        [slcvc setInitialText:tf.text];
        [self presentViewController:slcvc animated:YES completion:NULL];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter - not logged in!" message:@"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:@"Too bad!" otherButtonTitles:nil];
        [alert show];
    }

这当然是在“文件拥有”的 IBAction 函数中(slcvc 是 SLComposeViewController,bim 是 UIImage,tf.text 是 UITextfield tf 的文本)。我之前已经发布过这段代码,只是它是单独工作的。如果我尝试使用它同时将图片发布到 Facebook 和 Twitter,我会收到以下错误:

Attempt to present <SLTwitterComposeViewController: 0xf6265e0> on <ViewController: 0x9476960> which is waiting for a delayed presention of <SLFacebookComposeViewController: 0x9432d70> to complete

(我仍然可以在 Facebook 上发帖,但不能在 Twitter 上发帖)

我确信会发生这种情况,因为一旦第一次发布(在这种情况下是 Facebook 的发布)完成后,SLComposeViewController 会将自己注册为可以再次自由操作。那么有没有办法让第二个帖子(Twitter 的那个)以某种方式等待用户发送第一个帖子(到 Facebook),然后为 Twitter 呈现帖子?提前感谢任何人的帮助或建议!

4

2 回答 2

0

试试下面的东西。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self presentViewController:fbViewController animated:YES completion:nil];
    }];
于 2014-10-29T12:23:28.387 回答
0

您将需要使用 SLComposeViewController 的完成处理程序。这在用户完成撰写帖子或取消帖子后调用:

[slcvc setCompletionHandler:^(SLComposeViewControllerResult result) {
    //POST TO TWITTER
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        slcvc2 = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [slcvc2 addImage:bim];
        [slcvc2 setInitialText:tf.text];
        [self presentViewController:slcvc animated:YES completion:NULL];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter - not logged in!" message:@"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:@"Too bad!" otherButtonTitles:nil];
        [alert show];
    }
}
于 2013-06-03T06:42:07.920 回答