2

I want to post a tweet to Twitter when user taps a button in the application. I don't want to use the TWTweetComposeViewController so that the user again need to tap the Send button. I want to post tweet on tap on a button inside the application. (Using iOS Twitter Framework)

Is there any way to do this ?

Thanks

specios
  • 25
  • 8

2 Answers2

2

Use below code to do post image and text without showing ViewContoller . This is called silent Post.

 - (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 ;
                    }

                }
            }

        }];

    }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
ManiaChamp
  • 849
  • 7
  • 22
  • It works for posting an image ... what if I want to post just a status ? without any images . . I tried out some modifications... but nothing works... – specios Dec 05 '13 at 09:34
  • @specios : remove these two statements NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"icon@2x.png"]); [request addMultipartData:imageData withName:@"media[]" type:@"image/jpeg" filename:@"image.jpg"]; – ManiaChamp Dec 06 '13 at 06:02
  • Please show me your code that you are using or give me the link of GIT or BIT Bucket so i can fix . – ManiaChamp Dec 07 '13 at 07:18
0

Try this:

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);
    }
NSPratik
  • 4,714
  • 7
  • 51
  • 81