我試圖發佈到Twitter沒有用戶交互(因爲這會強制用戶多次點擊「發送」)。如何通過iOS向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的StatusCode。但是,當發送多個,我得到403和200,或只有403.
我該如何解決這個問題?
我不會試圖發送垃圾郵件。 「垃圾郵件通常被認爲是電子垃圾郵件或垃圾新聞組的帖子,有些人甚至將垃圾郵件定義爲任何未經請求的郵件。」我只發佈用戶想要發佈的內容。但是,我不想讓他們捱打發送n次,一旦批准發送,我想發送他們告訴我發送的內容。 –