Objective C - SLComposeViewController延迟呈现

时间:2013-05-19 04:34:26

标签: objective-c facebook twitter postdelayed slcomposeviewcontroller

我一直在做一个程序,负责在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文本)。我以前发过这个代码,只是它分开工作。如果我尝试使用此功能同时向Facebook和Twitter发布图片,我会收到此错误:

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

(我仍然可以发布到Facebook而不是Twitter)

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

2 个答案:

答案 0 :(得分: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];
    }
}

答案 1 :(得分:0)

试试下面的事情。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self presentViewController:fbViewController animated:YES completion:nil];
    }];