2012-11-11 26 views

Trả lời

59

Trên iOS 6, bạn nên sử dụng Social.framework. Điều này có một lớp học có tên là SLRequest.

Bạn sử dụng nó gần giống như cách không dùng nữa TWRequest, nhưng bạn cần xác định rằng đó là yêu cầu twitter thay vì yêu cầu facebook.

Toàn bộ Twitter.framework không còn được dùng như iOS 6, kể từ khi Apple thêm Facebook và Weibo (mạng xã hội Trung Quốc) vào iOS 6, họ đã nhóm tất cả các lớp xã hội vào Social.framework mới.

Lưu ý bạn phải xác định loại dịch vụ cho Twitter/Facebook, Ví dụ:

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

Hãy chắc chắn để kiểm tra documentation.

2

Đây là một mã hoàn chỉnh để tải văn bản + hình ảnh vào tài khoản Twitter của bạn sử dụng 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: Sử dụng "https://api.twitter.com/1.1/statuses/update.json" và cũng lưu ý rằng lỗi hoàn toàn có thể có thể trở lại như JSON trong responseData. – prewett

0

Trong trường hợp bạn có kế hoạch tích hợp TwitterKit bởi Twitter để thực hiện các tweets thông qua ứng dụng twitter tùy chỉnh của bạn thì đây có thể giúp bạn.

https://stackoverflow.com/a/28602749/1740354

0

Một cách khác là sử dụng API Twitter. Bạn nên có khuôn khổ Twitter cho điều đó.

Sau đó làm đoạn mã sau:

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); 
    } 
Các vấn đề liên quan