gfcf14
gfcf14

Reputation: 350

Objective C - SLComposeViewController delayed presention

I've been doing a program which takes care of posting a picture or words (or both) to both facebook and twitter. But I want to do them both at the same time, so I wrote my code like this:

//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];
    }

This is all of course, in an IBAction function that is "file owned" already (slcvc is the SLComposeViewController, bim is an UIImage, and tf.text is the text of the UITextfield tf). And I have posted with this code before, only that it worked separately. If I try to use this to post a picture to Facebook and Twitter at the same time, I get this error:

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

(I'm still allowed to post to Facebook but not to Twitter)

I'm sure this happens because the SLComposeViewController registers itself as free to operate again once the first posting (the one for Facebook in this case) is done. So is there a way to have the second posting (the one for Twitter) to wait somehow for the user to send the first posting (to Facebook) and then to present the posting for Twitter? Thanks to anyone in advance for any help or suggestions!!

Upvotes: 0

Views: 957

Answers (2)

nilkash
nilkash

Reputation: 7536

Just try following thing.

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

Upvotes: 0

Valent Richie
Valent Richie

Reputation: 5226

You will need to use the completion handler of the SLComposeViewController. This is called after user is done composing the post or cancelling it:

[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];
    }
}

Upvotes: 0

Related Questions