Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a direct message with the iOS 5 Twitter Framework?

I'm trying to use this code, and these instructions to do direct messages. Posting a normal tweet works perfectly fine, but when I try to send a direct message I get a 406.

This is the full code:

ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    // Did user allow us access?
    if (granted == YES)
    {
        // Populate array with all available Twitter accounts
        NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

        // Sanity check
        if ([arrayOfAccounts count] > 0) 
        {
            // Keep it simple, use the first account available
            ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

            // Build a twitter request
            NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/direct_messages/new.format"];
            NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:
                @"UserName",                    @"screen_name",
                @"Super awsome direct message", @"text",
                nil
            ];

            TWRequest *postRequest = [[TWRequest alloc]
                initWithURL:   url
                parameters:    p
                requestMethod: TWRequestMethodPOST
            ];

            // Post the request
            [postRequest setAccount:acct];

            // Block handler to manage the response
            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                CCLOG(@"Response Data\n%@", responseData);
                if (!error)
                    CCLOG(@"%@", [error description]);
            }];
        }
    }
}];
like image 768
Jay Avatar asked Apr 20 '12 21:04

Jay


People also ask

How do you Direct Message on Twitter IOS?

Step 1: Open the Twitter app on your device. Step 2: Tap on the envelope icon from the bottom menu. You will be directed to your Twitter DM history section.

Why can't I send a Direct Message to someone on Twitter?

Why am I having trouble sending Direct Messages? There is an account limit of 1,000 Direct Messages sent per day. Once you reach this limit, you can't send any more Direct Messages for the day. If you are sending Direct Messages to accounts that do not follow you, you may need to verify your phone number.

How do you send someone a Direct Message on Twitter?

Click on the share icon from a Tweet on your Home timeline or from a Tweet detail. Note: A protected Tweet cannot be shared through a Direct Message. Select Send via Direct Message. From the pop-up menu, Enter a name of the person you wish to send the message to or choose from the suggested account list.


1 Answers

Try using

https://api.twitter.com/1/direct_messages/new.json

instead of

https://api.twitter.com/1/direct_messages/new.format

like image 63
Robin Avatar answered Oct 21 '22 14:10

Robin