TwitterAgent Tutorial : Tweet from iPhone app in one line code with auto tinyURL

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

** For sample project, see the comments at the end of this post **

Perhaps, you have used the FacebookAgent in your apps. I gratefully thank the fellows from  iphonedevsdk.com and other iPhone developers who mailed me saying the class helped them. These are actual inspiration of sharing code with all.

Now, I would like to share my TwitterAgent class with you. Using TwitterAgent you can now integrate twitter in your iPhone app seamlessly. Additionally, you can also specify a URL and can make it tinyURL on the fly just passing an extra parameter!

Here are the steps:

1. Download TwitterAgent and BusyAgent and add them into your project.

2. Import TwitterAgent.h in the file where we want to use

3. Now call any of these function, they are self explanatory!

[[TwitterAgent defaultAgent] twit ];

[[TwitterAgent defaultAgent] twit:@"Search with google!" ];

[[TwitterAgent defaultAgent] twit:@"Search with google!" withLink:@"http://www.google.com" makeTiny:NO];

[[TwitterAgent defaultAgent] twit:@"Search with google!" withLink:@"http://www.google.com" makeTiny:YES];

Screenshots:

Login To Twitter

You only need to login once in one session. Login information will be saved to the device and will be used next time the application starts.

[[TwitterAgent defaultAgent] twit];
[[TwitterAgent defaultAgent] twit:@"Search with google!"];
[[TwitterAgent defaultAgent] twit:@"Search with google!" withLink:@"http://www.google.com" makeTiny:NO];
[[TwitterAgent defaultAgent] twit:@"Search with google!" withLink:@"http://www.google.com" makeTiny:YES];

Hope it will come into your help. Cheers!




Add to Google Reader or Homepage


  • does this use basic http authentication?
  • yea
  • Jackie
    When you use landscape mode, and when you click into the textview to edit message, then the keyboard will pop up and will cover the "send"button. However, there is no way to dismiss it after finishing edit. I tried to figure that out. And you may have a try.
    In the following function:
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    const char* str = [text UTF8String];

    int s = str[0];
    if(s!=0){

    if((range.location + range.length) > maxCharLength){
    return NO;
    }
    else{
    int left = 139 - ([sharedLink length] + [textView.text length]);
    lblCharLeft.text= [NSString stringWithFormat:@"%d",left];
    if([text isEqualToString:@"\n"]){

    [textView resignFirstResponder];
    return FALSE;

    }
    else {
    return YES;
    }
    }
    }

    int left = 139 - ([sharedLink length] + [textView.text length]);
    lblCharLeft.text= [NSString stringWithFormat:@"%d",left];
    return YES;
    }
  • Vic
    This is great piece of code. When I integrate it in my App, the user selects "post to twitter", I call the twit:withLink:makeTiny: method which logs the user in and posts the tweets. Now the users stays logged in. I would like to allow the user to logout also. The user may want to log out of that account and log in to another account for a future tweet. Is there a way to add a Logout button on the "tweet this post" view?
  • Thanks Vic for using it.

    I was thinking the same about logout! I will be adding soon, if you do this in the meantime, I will really appreciate if you share.

    Thanks.
  • Rob
    Is there anyway to include an image in the tweet? like from a screen shot?
  • no, not yet
  • Rob
    Thanks for the quick reply. Back to the drawing board. But this is a great drop in group of code. Thanks for your hard work.
  • leo888
    FacebookAgent and TwitterAgent are very cool,
    Is very easy to use, packaged very well.
    Thank you for sharing such a good code, so that Facebook and Twitter's programming has become so relaxed and happy.

    Very much hope that TwitterAgent can easily upload Image, hope to see even better updates, Thanks again.
  • thanks leo :)
  • uriash
    Wow!
    I don't have enough words to thank you for these great agents! You are one of a kind! I really mean it.
    Now a tiny question: when the user edits the text field of the tweet, how can he get out of edit mode and turn the keyboard down? the return button didn't work for me (it just jumped to the next line) and there was no "done" button.
  • Thank you for your generosity, I'm very happy that it came into your help.


    yea, u r right, but there is a "send" button on the alert box, need to tap the "send" button when done.
  • uriash
    True, but in a landscape environment you can't reach the "send" button cause it is obscured by the keyboard..
  • oops! I didn't test on landscape mode. Currently I'm fully busy in developing our next game at playdom.... dunno when i can manage time to work on this :( If you find a solve, please share.
  • exe
    Thanks! This is really helpful!
    One little possible bug: in makeTiny, url create from stringWithFormat seems miss one percent symbol?
  • thanks. And big thanx for identifying the bug :)
  • Hey, Thank you very much!
    Can you post a sample for this project? because the
    defaultAgent doesn't seems to be in here.

    Thanks.
  • seems answered the same person ( here and there ) :)
  • A sample project: http://amanpages.com/wordpress/wp-content/uploa...

    Another person in iphonedevsdk also faced the same prob.. while adding the TwitterAgent and BusyAgent folders, you have to select copy.

    Let me know if there is still any problem.
    Thanks :).
  • Jabroni
    This is outstanding, One question. Is there a way you can default the text and not allow the user to change it?
  • hi Jabroni, thanks :)

    At this moment its not possible. It will be there just after a while if you tell me the use case:


    1. user taps "share with twitter"
    2. logs in.
    3. The message will be shown to the user in alert but not in textarea i.e. not-editable. Tapping send button will send, cancel cancels.

    or

    1. user taps "share with twitter"
    2. logs in.
    3. No alert, only gray busy overlay. Posts the twit in background?


    or

    Both?

    Thank you for your suggestion. I will integrate it just let me know.
blog comments powered by Disqus