How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance

五迷三道 提交于 2019-11-27 22:36:36
 - (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
}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!