3

My app needs to support iOS 5.

I have my custom UI where user can enter tweet message and when he presses post button, It should post message twitter.

I have already written code for posting via SLComposeViewController *tweetSheet instance but in this case i cannot directly press send button presented by tweetSheet without presenting it by

[self presentViewController:tweetSheet animated:YES completion:nil];

Is it possible to bypass this presentation and set text message and post to twitter via my custom ui which has post button ??

  if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];


        tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
            switch(result) {
                    //  This means the user cancelled without sending the Tweet
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"Tweet message was cancelled");
                    break;
                    //  This means the user hit 'Send'
                case SLComposeViewControllerResultDone:
                    NSLog(@"Done pressed successfully");
                    break;
            }

            //  dismiss the Tweet Sheet
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:NO completion:^{
                    NSLog(@"Tweet Sheet has been dismissed.");
                }];
            });
        };

        [tweetSheet setInitialText:self.textViewPostedText.text];

        [self presentViewController:tweetSheet animated:YES completion:nil];
    }

How to give select option if user have multiple Twitter accounts ???

bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
  • see this link this worked for me http://stackoverflow.com/questions/9423447/ios-5-twitter-framework-tweeting-without-user-input-and-confirmation-modal-vie – Shankar BS Aug 27 '13 at 07:27
  • in the above link that completely avoids the use of SLComposeViewController u can directly post to twitter wall – Shankar BS Aug 27 '13 at 07:39
  • I am getting error "Undefined symbols for architecture i386: "_OBJC_CLASS_$_TWRequest", referenced from:" i have already included #import #import #import #import and required framework – bhavya kothari Aug 27 '13 at 07:42
  • in which line u are getting this error – Shankar BS Aug 27 '13 at 07:47
  • and check did u also set user twitter account in device settings – Shankar BS Aug 27 '13 at 07:54
  • Code is running fine but getting error " void _WebThreadLockFromAnyThread(bool), 0xac2f080: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread., by NSLog i am also getting "Tweet failed" message" – bhavya kothari Aug 27 '13 at 07:56
  • i hav used the same code but bit modified, i did nt get any error's wait i am goona post my code – Shankar BS Aug 27 '13 at 08:00
  • I am able to run my project successfully but i am not getting tweet message posted to my account instead getting above message in debug area – bhavya kothari Aug 27 '13 at 08:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36305/discussion-between-bhavya-kothari-and-shan) – bhavya kothari Aug 27 '13 at 08:01

1 Answers1

7
 - (IBAction)doneButtonClicked:(id)sender
  {
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSString *message = _textView.text;
    //hear before posting u can allow user to select the account
    NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
    for(ACAccount *acc in arrayOfAccons)
    {
       NSLog(@"%@",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview 
    } 
in below 


 // Request access from the user to access their Twitter account
  [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
  {
     if (granted == YES)
      {
         // Populate array with all available Twitter accounts
         NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
         if ([arrayOfAccounts count] > 0)
         {
             //use the first account available
             ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)

             //Build a twitter request
             TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                                       [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]
                                                          parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"] requestMethod:TWRequestMethodPOST];//for iOS 7 


              //for iOS 6 use "https://api.twitter.com/1/statuses/update.json"
              //Post the request
              //u should get the response code 200 for successful post
             [postRequest setAccount:acct];

             //manage the response
             [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
              {
                 if(error)
                  {
                      //if there is an error while posting the tweet
                      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                      [alert show];
                      [alert release];
                  }
                  else
                  {
                      // on successful posting the tweet
                      NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                      [alert show];
                      [alert release];

                  }
              }];
             [postRequest release];
         }
         else
         {
             UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alert show];
             [alert release];
         }
     }
     else
     {
         //suppose user not set any of the accounts
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [alert show];
         [alert release];
     }
 } ];

[account release]; //for non-ARC
}


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • how can i give user option to select twitter account to which tweet message must be posted ??? – bhavya kothari Aug 27 '13 at 13:35
  • Above code works perfectly fine, only thing how can we provide list accounts user has pre-configured on its device and by selecting anyone account to which tweet message will be posted – bhavya kothari Aug 27 '13 at 13:45
  • that u can check in NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; this method, since it gives all the accounts that user has configured in his device and based on this u can select the account to post, i will check and update ok – Shankar BS Aug 27 '13 at 15:37
  • Hmm i checked, and i updated my answer check, let me know if there are any difficulties :) – Shankar BS Aug 28 '13 at 04:52
  • hey its not working for me , I am getting following response "Twitter HTTP response: 403" – ViruMax Feb 05 '14 at 07:43
  • ya sorry for delay, it might be there is small change in url hear try withis url, `http://api.twitter.com/1.1/statuses/update.json` for ios 6 and `https://api.twitter.com/1.1/statuses/update.json` for iOS 7, u should get response code 200 – Shankar BS Feb 05 '14 at 09:32
  • hmm api version was the problem, current api version for twitter is 1.1, thanx – ViruMax Feb 05 '14 at 10:08