2012-07-03 8 views

risposta

18

Utilizzare il seguente codice per il retweet.

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

Good Luck.

+0

Questo ha aiutato? –

+1

sì, grazie funziona per il retweet, ora sto cercando il preferito e rispondo – PJR

+0

accetterò con aria di sfida la risposta, ho già votato, ma sto cercando il preferito e rispondo, ecco perché sto aspettando :) :) – PJR

1

Utilizzare la classe TWRequest anziché TWTweetComposeViewController per accedere a tweet, retweet, rispondi e preferiti. Per maggiori informazioni usa questi link. Link1 e Link2

+0

grazie , ma quale URL e parametri devo passare per il retweet, la risposta e il preferito? – PJR

+0

@PJR controlla la mia risposta. –

9

amici risposta Jennis s' mi aiuta per trovare le mie soluzioni e risposta è:

1) Per Retweet e preferita

Si tratta di un metodo di classe per l'invio di richiesta, devi passare diversi URL per retweet e preferiti.

+ (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 per Retweet:
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
URL preferita:
https://api.twitter.com/1/favorites/create/id_str.json
https://api.twitter.com/1/favorites/destroy/id_str.json

2) Codice di risposta

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

metodo di chiamata:

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

Ho creato una classe iOS5Twitter e implementare un metodo di classe, se si vuole farlo in tuo controller, gentilmente sostituire delegate da self

+0

Ciao PJR .. Sto ricevendo 'code = 34; message = "Spiacente, quella pagina non esiste"; 'mentre si passa https://api.twitter.com/1/favorites/create/id_str.json url. Potete per favore aiutarmi a fare questo? –

+0

@ShahPaneri potrebbe essere un problema di modifica nella libreria.Penso che ora un giorno twitter abbia cambiato la sua libreria, quindi guardala, potrebbe aiutarti. – PJR

+0

Ho trovato il problema. Non ho autenticato l'utente, quindi mi ha dato l'errore. B.T.W. grazie per la risposta. :) –

Problemi correlati