Jason
Jason

Reputation: 14605

How to determine if ios user is signed in to twitter?

I want to determine it a user is signed on to twitter, so I can prompt them to post a tweet but only if they are set up with ios and twitter. Also, is there a way to set up the twitter screen with a default tweet?

Upvotes: 0

Views: 1260

Answers (2)

tipycalFlow
tipycalFlow

Reputation: 7644

For iOS 5.x, you can check if the user is signed in to twitter using:

[TWTweetComposeViewController canSendTweet]

As for presenting a tweet screen with default message, you can do something like:

TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:defaultMsg];
[self presentModalViewController:tweetSheet animated:YES];

You can refer this tutorial for more.

EDIT: For iOS 6.0 and above, use:

// requires "Social.framework"
[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]

So, an example usage could be:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
            [controller dismissViewControllerAnimated:YES completion:Nil];
        };
        controller.completionHandler = myBlock;
        [controller setInitialText:@"#myHashTag"];
        [controller addImage:myImage];
        [self presentViewController:controller animated:YES completion:Nil];
    }
    else
    { /* Show error alert, etc*/ }

Further reading

Upvotes: 5

SpaceDust__
SpaceDust__

Reputation: 4914

As I said in my comment if you are using IOS 5 or later versions just add Twitter.framework to your projects

#import <Twitter/Twitter.h>

//post tweets
- (IBAction)postTapped:(id)sender{

    if ([TWTweetComposeViewController canSendTweet])
    {
        TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
        [tweetSheet setInitialText:@" #hashtag"];


        [self presentModalViewController:tweetSheet animated:YES];
    }
    else
    {

        TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];

        //hide the tweet screen
        viewController.view.hidden = YES;

        //fire tweetComposeView to show "No Twitter Accounts" alert view on iOS5.1
        viewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
            if (result == TWTweetComposeViewControllerResultCancelled) {
                [self dismissModalViewControllerAnimated:NO];
            }
        };
        [self presentModalViewController:viewController animated:NO];

        //hide the keyboard
        [viewController.view endEditing:YES];
    }


}

Upvotes: 2

Related Questions