DerrickHo328
DerrickHo328

Reputation: 4886

Making Post Request to twitter with IOS

I am new to the concept of POSTing to twitter. I've looked here in order to understand how GET something from twitter. But I can't find any example about how to POST something to twitter.

how do I tell twitter that userXYZ is posting a tweet? How about the text body of the tweet itself.

Again, I'm very new to the concept.

Upvotes: 0

Views: 66

Answers (3)

DerrickHo328
DerrickHo328

Reputation: 4886

Thanks Rashad. This is what I am using now. It works.

- (void)testPost {

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
     {
       if (granted)
         {
           NSArray *accounts = [accountStore accountsWithAccountType:accountType];
           if (accounts.count)
             {
               NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
               [dict setValue:@"check Check CHECK" forKey:@"status"];

               NSString *retweetString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/update.json"];
               NSURL *retweetURL = [NSURL URLWithString:retweetString];
               SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:retweetURL parameters:dict];

               request.account = [accounts objectAtIndex:0];

               [request performRequestWithHandler:^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error)
                {

                  if (responseData1)
                    {

                      NSError *error1 = nil;
                      id response = [NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableLeaves error:&error1];

                      NSLog(@"%@", response);    
                    }
                }];
             }
         }

     }];
}

Upvotes: 0

Hardik Kardani
Hardik Kardani

Reputation: 576

Using Twitter SDK import #import<Twitter/Twitter.h> in your .h or .m file


if ([TWTweetComposeViewController canSendTweet])
{

    NSURL* aURL = [NSURL URLWithString:@"your image Url"];
    NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
    UIImage *img1 = [UIImage imageWithData:data];
    // Initialize Tweet Compose View Controller
    TWTweetComposeViewController *vc = [[TWTweetComposeViewController alloc] init];
    // Settin The Initial Text

    [vc setInitialText:@"Setting The Initial Text"];
    [vc addImage:img1];
    // Adding a URL
    strWEBTwitter = @"Pass Your URL here"
    [vc addURL:[NSURL URLWithString:strWEBTwitter]];
    // Setting a Completing Handler
    [vc setCompletionHandler:^(TWTweetComposeViewControllerResult result)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"Result : %d", result);
        if (result == TWTweetComposeViewControllerResultDone)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Thanks for sharing." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];                [alertView show];

        }

        else if (result == TWTweetComposeViewControllerResultCancelled)
        {
            NSLog(@"Cancle")
        }

    }];
    // Display Tweet Compose View Controller Modally
    [self presentViewController:vc animated:YES completion:nil];
} else
{
    // Show Alert View When The Application Cannot Send Tweets
    NSString *message = @"There are no Twitter accounts configured. You can add or create a Twitter account in Settings.";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"No Twitter Accounts" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
    [alertView show];
}

Upvotes: 0

Rashad
Rashad

Reputation: 11197

You can easily find those in API Documentation. I think you are looking for this.

Also there are some question already asked in SO. You can also check this.

Upvotes: 1

Related Questions