-(void)twitter{
ACAccountStore *account=[[ACAccountStore alloc] init]; //本机账户数据库, 储存weibo,facebook等各种账户
ACAccountType *accountType=[account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSLog(@"accountType:%@",accountType);
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"认证通过");
if (!self.content.text) {
UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入要发分享的内容" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[a show];
return ;
}
NSArray *arrayAccounts=[account accountsWithAccountType:accountType];
NSLog(@"arrayAccounts:%@",arrayAccounts);
if ([arrayAccounts count]>0) {
// NSLog(@"认证通过");
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"]
parameters:@{@"status": self.content.text}];
NSLog(@"account:%@",[[arrayAccounts objectAtIndex:0] class]);
request.account=[arrayAccounts objectAtIndex:0] ;
[request addMultipartData:self.imageData withName:@"media[]" type:@"multipart/form-data" filename:@"a.png"];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
id jsonObj = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments error:&error];
NSLog(@"jsonObj:%@",jsonObj);
if (!error) {
NSLog(@"%@",[jsonObj objectForKey:@"statuses"]) ; //key更具api json决定
UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提示" message:@"分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[a show];
}
}];
}else{
UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提示" message:@"无效账户,请到设置界面设置账号信息" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[a show];
}
}
}];
}