2012-07-03 18 views

Respuesta

18

Use el siguiente código para retweeting.

- (void)_retweetMessage:(TwitterMessage *)message 
{ 
    NSString *retweetString = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/retweet/%@.json", message.identifier]; 
    NSURL *retweetURL = [NSURL URLWithString:retweetString]; 
    TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:nil requestMethod:TWRequestMethodPOST]; 
    request.account = _usedAccount; 

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     if (responseData) 
     { 
      NSError *parseError = nil; 
      id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError]; 

      if (!json) 
      { 
       NSLog(@"Parse Error: %@", parseError); 
      } 
      else 
      { 
       NSLog(@"%@", json); 
      } 
     } 
     else 
     { 
      NSLog(@"Request Error: %@", [error localizedDescription]); 
     } 
    }]; 
} 

Taken From Here

buena suerte.

+0

¿Esto ayudó? –

+1

sí, gracias funciona para retweet, ahora estoy intentando para el favorito y respondo – PJR

+0

definitivamente aceptaré su respuesta, ya lo he votado, pero estoy tratando de encontrar el favorito y responder, es por eso que estoy esperando :) – PJR

1

Usa la clase TWRequest en lugar de TWTweetComposeViewController para acceder a los tweets, retweets, respuestas y favoritos. Para más información use estos enlaces. Link1 y Link2

+0

gracias , pero ¿qué URL y parámetros debo aprobar para retweet, reply y favorite? – PJR

+0

@PJR verifica mi respuesta. –

9

amigos respuesta Jennis 's me ayuda para encontrar mis soluciones y respuesta es:

1) Para Retweet y favorita

Este es un método de clase para el envío de la solicitud, usted tiene que pasar diferentes URL para retweet y Favorito.

+ (void)makeRequestsWithURL: (NSURL *)url { 
    // Create an account store object. 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Request access from the user to use their Twitter accounts. 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 
     if(granted) { 
      // Get the list of Twitter accounts. 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

      // For the sake of brevity, we'll assume there is only one Twitter account present. 
      // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 


       // Create a request, which in this example, posts a tweet to the user's timeline. 
       // This example uses version 1 of the Twitter API. 
       // This may need to be changed to whichever version is currently appropriate. 
       TWRequest *postRequest = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST]; 

       // Set the account used to post the tweet. 
       [postRequest setAccount:twitterAccount]; 

       // Perform the request created above and create a handler block to handle the response. 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
        NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
        iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init]; 
        [twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; 
        [twitter5 release];    }]; 
      } 
     } 

    }]; 

} 

URL para Retweet:
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
URL de favorito:
https://api.twitter.com/1/favorites/create/id_str.json
https://api.twitter.com/1/favorites/destroy/id_str.json

2) Código de respuesta

 + (void)makeRequestForReplyWithSelectedFeed:(NSString *)status inReply2:(NSString *)updateID{ 
    // Create an account store object. 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 



    NSString *trimmedText = status; 
    if ([trimmedText length] > 140.0) { 
     trimmedText = [trimmedText substringToIndex:140]; 
    } 

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0]; 
    [params setObject:trimmedText forKey:@"status"]; 
    if (updateID > 0) { 
     [params setObject:[NSString stringWithFormat:@"%@", updateID] forKey:@"in_reply_to_status_id"]; 
    } 

    // Request access from the user to use their Twitter accounts. 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 
     if(granted) { 
      // Get the list of Twitter accounts. 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

      // For the sake of brevity, we'll assume there is only one Twitter account present. 
      // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 


       // Create a request, which in this example, posts a tweet to the user's timeline. 
       // This example uses version 1 of the Twitter API. 
       // This may need to be changed to whichever version is currently appropriate. 
        TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:params requestMethod:TWRequestMethodPOST]; 

       // Set the account used to post the tweet. 
       [postRequest setAccount:twitterAccount]; 

       // Perform the request created above and create a handler block to handle the response. 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
        NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
        iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init]; 
        [twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; 
        [twitter5 release];    }]; 
      } 
     } 
    }]; 
} 

método de llamada:

[iOS5Twitter makeRequestForReplyWithSelectedFeed:[NSString stringWithFormat:@"%@", tweetMessage.text ]inReply2:[ NSString stringWithFormat:@"%@",selectedFeed.id_str]];

He creado una clase iOS5Twitter y poner en práctica un método de clase, si desea hacerlo en su controlador, sustitúyase delegate por self

+0

Hola PJR .. Estoy obteniendo 'code = 34; message = "Lo siento, esa página no existe"; 'al pasar https://api.twitter.com/1/favorites/create/id_str.json url. ¿Puedes ayudarme a hacer esto? –

+0

@ShahPaneri puede ser un problema de cambio en la biblioteca.Creo que ahora Twitter ha cambiado su biblioteca, así que mira eso, puede ayudarte. – PJR

+0

Encontré el problema. No he autenticado al usuario, así que me estaba dando el error. B.T.W. gracias por responder. :) –

Cuestiones relacionadas