如何通过iOS向Twitter发布多个帖子?

如何通过iOS向Twitter发布多个帖子?,ios,twitter,http-status-code-403,twrequest,Ios,Twitter,Http Status Code 403,Twrequest,我试图在没有用户交互的情况下发布到Twitter(因为这会迫使用户多次点击“发送”) 以下是我的代码: - (void) postToTwitterUsingTWRequest: (NSDictionary*) appDictionary { NSString *trackName = [appDictionary objectForKey:@"trackName"]; NSString *trackId = [[appDictionary objectForKey:@"trackId"]

我试图在没有用户交互的情况下发布到Twitter(因为这会迫使用户多次点击“发送”)

以下是我的代码:

- (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];


        }

    }
}];
}
我通过这个循环进行多次调用,如下所示:

 for (NSDictionary* appDictionary in selectedApps) {

    [self postToTwitterUsingTWRequest:appDictionary];

}
有时它允许我发送一个给我200状态码。但是当发送多个时,我得到403和200,或者只有403


如何解决此问题?

在继续之前,您应该阅读以下链接,您试图做的事情称为垃圾邮件

试试这个

AT.H

#导入
#进口
-(无效)发送推特
{
类tweeterClass=NSClassFromString(@“tweetComposeViewController”);
如果(tweeterClass!=nil){//检查Twitter集成
if([TWTweetComposeViewController canSendTweet])
{
//创建帐户存储,后跟twitter帐户标识符
//目前,twitter是唯一可用的帐户类型
ACAccountStore*account=[[ACAccountStore alloc]init];
ACAccountType*accountType=[帐户类型WithAccountTypeIdentifier:ACAccountTypeIdentifierWitter];
//请求用户访问其Twitter帐户
[account requestAccessToAccountsWithType:accountType with CompletionHandler:^(已授予BOOL,N错误*错误)
{
//用户允许我们访问吗?
如果(已授予==是)
{
//用所有可用的Twitter帐户填充数组
NSArray*arrayOfAccounts=[account accountsWithAccountType:accountType];
//健康检查
如果([arrayOfAccounts]>0)
{
//保持简单,使用第一个可用帐户
ACAccount*acct=[arrayOfAccounts objectAtIndex:0];
//构建一个twitter请求
TWRequest*postRequest=[[TWRequest alloc]initWithURL:
[NSURL URLWithString:@”http://api.twitter.com/1/statuses/update.json"]
参数:[NSDictionary Dictionary WithObject:str\u tweet
forKey:@“status”]requestMethod:TWRequestMethodPOST];
//发布请求
[请求后设置帐户:acct];
//用于管理响应的块处理程序
[postRequest performRequestWithHandler:^(NSData*responseData,NSHTTPURLRResponse*urlResponse,NSError*错误)
{
//NSString*response=[[NSString alloc]initWithData:responseData编码:NSUTF8StringEncoding];
//NSLog(@“Twitter响应:%@,HTTP响应:%@”,响应,[urlResponse statusCode]);
}];
}
}
}];
}
其他的
{
NSLog(@“无法发送tweet!”);
}
}
}
我没有试图发送垃圾邮件。“垃圾邮件通常被认为是电子垃圾邮件或垃圾新闻组帖子。有些人更普遍地将垃圾邮件定义为任何未经请求的邮件。”我只发布用户想要发布的内容。然而,我不想让他们点击发送n次而惹恼他们,一旦他们批准发送,我想发送他们让我发送的内容。
#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!");
            }
        }
    }