2012-11-11 17 views

risposta

59

Su iOS 6 è necessario utilizzare lo Social.framework. Questo ha una classe chiamata SLRequest.

Lo si utilizza quasi allo stesso modo del deprecato TWRequest, ma è necessario specificare che si tratta di una richiesta di Twitter in contrasto con una richiesta di Facebook.

L'intero Twitter.framework è diventato obsoleto a partire da iOS 6, poiché Apple ha aggiunto Facebook e Weibo (un social network cinese) a iOS 6, raggruppando tutte le classi sociali nel nuovo Social.framework.

Nota è necessario specificare il tipo di servizio a Twitter/Facebook, Esempio:

SLRequest *aRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter 
              requestMethod:SLRequestMethodPOST 
                URL:myurl 
              parameters:myparams]; 

essere sicuri di controllare la documentation.

2

Ecco un codice completo per caricare il testo + immagini al tuo account Twitter utilizzando Twitter API

UIImage *img = [UIImage imageNamed:@"twitterImage.png"]; 
    ACAccountStore *account = [[ACAccountStore alloc] init]; 
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [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) { 
       ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; 
       NSDictionary *message = @{@"status": @"From my app"}; 
       NSURL *requestURL = [NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"]; 
       SLRequest *postRequest = [SLRequest 
                requestForServiceType:SLServiceTypeTwitter 
                requestMethod:SLRequestMethodPOST 
                URL:requestURL parameters:message]; 
       NSData *data = UIImagePNGRepresentation(img); 
       [postRequest addMultipartData:data withName:@"media" type:@"image/png" filename:@"TestImage"]; 
       postRequest.account = acct; 

       [postRequest performRequestWithHandler: 
        ^(NSData *responseData, NSHTTPURLResponse 
         *urlResponse, NSError *error) 
        { 
         if (error) { 
          NSLog(@"%@",error.description); 
         } 
         else { 
          NSLog(@"Upload Sucess !"); 
         } 
        }]; 
      } 
     } 
    }]; 
+1

2015: Usa "https://api.twitter.com/1.1/statuses/update.json" e anche notare che gli errori molto probabilmente possono tornare come JSON in il responseData. – prewett

0

Un'altra alternativa è di usare Twitter API. Dovresti avere il framework di Twitter per questo.

Poi do seguente codice:

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); 
    } 
Problemi correlati