简体   繁体   中英

How to make multiple posts to Twitter via iOS?

I am attempting to post to Twitter without user interaction (as this would force the user to hit 'Send' multiple times.).

The following is my code:

- (void) postToTwitterUsingTWRequest: (NSDictionary*) appDictionary {


NSString *trackName = [appDictionary objectForKey:@"trackName"];
NSString *trackId = [[appDictionary objectForKey:@"trackId"] description];
NSString *artworkUrl512 = [appDictionary objectForKey:@"artworkUrl512"];

NSMutableString *requestUrlString = [NSMutableString new];
[requestUrlString appendFormat:@"http://itunes.apple.com/%@",[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
[requestUrlString appendFormat:@"/app/%@", trackName];
[requestUrlString appendFormat:@"/id%@?mt=8", trackId];

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

[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {

    if (granted == YES) {

        NSArray *arrayOfAccounts = [account
                                    accountsWithAccountType:accountType];

        if ([arrayOfAccounts count] > 0)
        {
            ACAccount *twitterAccount = [arrayOfAccounts lastObject];

            TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];

            //NSData *tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://eborkdev.com/wp-content/uploads/2012/05/logo.png"]];
            NSData *tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString: artworkUrl512]];
            [postRequest addMultiPartData:tempData withName:@"media" type:@"image/png"];

            tempData = [[NSString stringWithFormat:@"%@ was recommended using Tell A Friend (http://link_to_tell_a_friend.com). \n %@", trackName, requestUrlString] dataUsingEncoding:NSUTF8StringEncoding];
            [postRequest addMultiPartData:tempData withName:@"status" type:@"text/plain"];

            [postRequest setAccount:twitterAccount];

            isPostingToTwitter = true;
            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

                isPostingToTwitter = false;
                NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]);

            }];

        }

        else {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"No Twitter accounts found. Please ensure that there are accounts present, and try again."
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alertView show];


        }

    }
}];
}

I am looping through this in order to make the multiple calls like so:

 for (NSDictionary* appDictionary in selectedApps) {

    [self postToTwitterUsingTWRequest:appDictionary];

}

Sometimes it allows me to send one giving me the 200 statusCode. But when sending multiple, I get 403 and 200, or just 403.

How can I fix this?

You should read the following links before proceeding, the thing you are trying to do is called spamming..

https://dev.twitter.com/docs/error-codes-responses

https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following

TRY THIS

AT .H

#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>

- (void)sendTweet
{
        Class tweeterClass = NSClassFromString(@"TWTweetComposeViewController");

        if(tweeterClass != nil) {   // check for Twitter integration
            if ([TWTweetComposeViewController canSendTweet])
            {
                // Create account store, followed by a twitter account identifier
                // At this point, twitter is the only account type available
                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
                             TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                                                   [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                                                                      parameters:[NSDictionary dictionaryWithObject:str_tweet
                                                                                                             forKey:@"status"] requestMethod:TWRequestMethodPOST];

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

                             // Block handler to manage the response
                             [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                              {
                                  // NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
                                  // NSLog(@"Twitter response: %@, HTTP response: %@", response, [urlResponse statusCode]);
                              }];
                         }
                     }
                 }];
            }
            else
            {
                NSLog(@"Unable to tweet!");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM