Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement