我想上传图片而不在ios5和ios6中打开Tweetsheet。 可能吗? 我曾尝试使用TweetSheet上传图片,我可以上传照片,但无法在不打开推文的情况下上传照片。
使用过: - TWTweetComposeViewController
请告诉我怎么做?
提前致谢
答案 0 :(得分:3)
if (appDelegate.PHONE_OS >= 5)
{
NSString *strTwitterMessage;
strTwitterMessage = [NSString stringWithFormat:@"test twitter post : Photo link URL "];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(@"Count : %d",[accountsArray count]);
ACAccount *twitterAccount;
int flagTwitter;
flagTwitter = 0;
for(int i = 0;i<[accountsArray count];i++)
{
if([[[accountsArray objectAtIndex:i]username] isEqualToString:appDelegate.strTwitterEmailSelected])
{
flagTwitter = 1;
twitterAccount = [accountsArray objectAtIndex:i];
}
}
if(flagTwitter == 1)
{
appDelegate.strTwitterEmail = appDelegate.strTwitterEmailSelected;
}
else
{
appDelegate.strTwitterEmail = [[accountsArray objectAtIndex:0]username];
twitterAccount = [accountsArray objectAtIndex:0];
}
//ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
//appDelegate.strTwitterEmail = [[accountsArray objectAtIndex:0]username];
UIImage *image =[UIImage imageNamed:@"image.png"];
NSURL *url =
[NSURL URLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];
// Create a POST request for the target endpoint
TWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodPOST];
[request setAccount:twitterAccount];
NSData *imageData = UIImagePNGRepresentation(image);
[request addMultiPartData:imageData
withName:@"media[]"
type:@"multipart/form-data"];
NSString *status = strTwitterMessage;
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"
type:@"multipart/form-data"];
[request performRequestWithHandler:
^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData1 options:0 error:nil];
// Log the result
NSLog(@"%@", dict);
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"status : %@", output);
dispatch_async(dispatch_get_main_queue(), ^{
// perform an action that updates the UI...
UIAlertView *customAlertView = [[UIAlertView alloc]initWithTitle:@"Social Media"
message:@"successful share"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK",nil];
[customAlertView show];
[customAlertView release];
});
}];
}
else
{
// Alert Please login through seeting
dispatch_async(dispatch_get_main_queue(), ^{
// perform an action that updates the UI...
UIAlertView *customAlertView = [[UIAlertView alloc]initWithTitle:@"Social Media"
message:@"Please enable twitter through iPhone Settings"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK",nil];
[customAlertView show];
[customAlertView release];
});
}
}];
}
答案 1 :(得分:0)
您可以使用MGTwitter Library 它的推文非常好 请在此链接中查看