使用Twitter框架从iOS应用程序自动发送推文-无需使用TWTweetComposeViewController
当用户点击应用程序中的按钮时,我想在Twitter上发布一条推文。我不想使用tweetcomposeviewcontroller,这样用户就需要再次点击发送按钮。我想在应用程序中点击一个按钮发布tweet。(使用iOS Twitter框架) 有没有办法做到这一点使用Twitter框架从iOS应用程序自动发送推文-无需使用TWTweetComposeViewController,ios,twitter,ios7,Ios,Twitter,Ios7,当用户点击应用程序中的按钮时,我想在Twitter上发布一条推文。我不想使用tweetcomposeviewcontroller,这样用户就需要再次点击发送按钮。我想在应用程序中点击一个按钮发布tweet。(使用iOS Twitter框架) 有没有办法做到这一点 谢谢使用下面的代码在不显示ViewContoller的情况下发布图像和文本。这被称为无声帖子。 - (void) shareOnTwitterWithMessage:(NSString *)message { ACA
谢谢使用下面的代码在不显示ViewContoller的情况下发布图像和文本。这被称为无声帖子。
- (void) shareOnTwitterWithMessage:(NSString *)message {
ACAccountStore *twitterAccountStore = [[ACAccountStore alloc]init];
ACAccountType *TWaccountType= [twitterAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[twitterAccountStore requestAccessToAccountsWithType:TWaccountType options:nil completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [twitterAccountStore accountsWithAccountType:TWaccountType];
twitterAccounts = [accounts lastObject];
NSDictionary *dataDict = @{@"status": message};
[self performSelectorInBackground:@selector(postToTwitter:) withObject:dataDict];
}
else {
return ;
}
}];
}
- (void)postToTwitter:(NSDictionary *)dataDict{
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:dataDict];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"icon@2x.png"]);
[request addMultipartData:imageData
withName:@"media[]"
type:@"image/jpeg"
filename:@"image.jpg"];
request.account = twitterAccounts;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if(!error){
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(![list objectForKey:@"errors"]){
if([list objectForKey:@"error"]!=nil){
//Delegate For Fail
return ;
}
}
}
}];
}
试试这个:
NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/update.json";
NSDictionary *params = @{@"status": @"Hello, my first autopost tweet..."};
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
URLRequestWithMethod:@"POST"
URL:statusesShowEndpoint
parameters:params
error:&clientError];
if (request) {
[[[Twitter sharedInstance] APIClient]
sendTwitterRequest:request
completion:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
if (data) {
// handle the response data e.g.
NSError *jsonError;
NSDictionary *dicResponse = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(@"%@",[dicResponse description]);
}
else {
NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
}
}];
}
else {
NSLog(@"Error: %@", clientError);
}
检查一下这个@kirtimali好的,让我看看。。谢谢…它可以用来发布图片。。。如果我只想发布一个状态呢?没有任何图像。我尝试了一些修改。。。但是没有任何效果…@specios:删除这两条语句NSData*imageData=UIImagePNGRepresentation([UIImage-imagename:@]icon@2x.png"]); [请求添加MultipartData:imageData,名称:@“media[]”类型:@“image/jpeg”文件名:@“image.jpg”];请告诉我你正在使用的代码,或者给我GIT或BIT Bucket的链接,以便我可以修复。