如果您正在使用ios 5,那麼下面的代碼會有幫助,因爲我通過此代碼發送圖像和文本。
首先是添加框架「twitter.framework」
NSString *var = txtTwitter.text;
NSString *str = [[NSUserDefaults standardUserDefaults]valueForKey:@"INDEX"];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];
//add text
[postRequest addMultiPartData:[var dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data"];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%@.jpg",str]];
//add image
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/form-data"];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"Twitter msg %@",output);
// [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
}];
}
}
}];
我使用MGTwitterEngine –
我在解決方案http://www.musicalgeometry.com/?p=1537找到。謝謝 –