2013-10-25 48 views
1

我想將圖片上傳到Twitter。Twitter無法完成操作。 (com.apple.accounts錯誤6.)?

我寫代碼

- (void)postImage:(UIImage *)image withStatus:(NSString *)status 
{ 
    NSLog(@"Share on Twitter"); 

    ACAccountType *twitterType = 
    [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    SLRequestHandler requestHandler = 
    ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     if (responseData) { 
      NSInteger statusCode = urlResponse.statusCode; 
      if (statusCode >= 200 && statusCode < 300) { 
       NSDictionary *postResponseData = 
       [NSJSONSerialization JSONObjectWithData:responseData 
               options:NSJSONReadingMutableContainers 
                error:NULL]; 
       NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]); 
      } 
      else { 
       NSLog(@"[ERROR] Server responded: status code %d %@", statusCode, 
         [NSHTTPURLResponse localizedStringForStatusCode:statusCode]); 
      } 
     } 
     else { 
      NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]); 
     } 
    }; 

    ACAccountStoreRequestAccessCompletionHandler accountStoreHandler = 
    ^(BOOL granted, NSError *error) { 
     if (granted) { 
      NSArray *accounts = [self.accountStore accountsWithAccountType:twitterType]; 
      NSURL *url = [NSURL URLWithString:@"https://api.twitter.com" 
          @"/1.1/statuses/update_with_media.json"]; 
      NSDictionary *params = @{@"status" : status}; 
      SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter 
                requestMethod:SLRequestMethodPOST 
                   URL:url 
                 parameters:params]; 
      NSData *imageData = UIImageJPEGRepresentation(image, 1.f); 
      [request addMultipartData:imageData 
          withName:@"media[]" 
           type:@"image/jpeg" 
          filename:@"image.jpg"]; 
      [request setAccount:[accounts lastObject]]; 
      [request performRequestWithHandler:requestHandler]; 
     } 
     else { 
      NSLog(@"[ERROR] An error occurred while asking for user authorization: %@", 
        [error localizedDescription]); 
     } 
    }; 

    [self.accountStore requestAccessToAccountsWithType:twitterType 
               options:NULL 
              completion:accountStoreHandler]; 
} 

我得到錯誤的

The operation couldn’t be completed. (com.apple.accounts error 6.) 

回答

0

您應該使用SLComposeViewController類。爲了做到這一點,應該按順序執行許多步驟。首先,應用程序可以選擇檢查以驗證消息是否可以發送到指定的社交網絡服務。這基本上等同於檢查是否有效的社交網絡賬戶已被在設備上配置和使用該isAvailableForServiceType:類方法實現,通過作爲一個參數從以下選項中所要求的服務的類型:

SLServiceTypeFacebook 
SLServiceTypeTwitter 
SLServiceTypeSinaWeibo 

以下代碼,例如,驗證Twitter服務是提供給應用程序:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) 
{ 
    // Device is able to send a Twitter message 
} 

調用這個方法是可選的,在活動,在指定的社交網絡帳戶尚待建立,作曲家將只需將用戶帶到設備的「設置」應用程序中即可Twitter帳戶可能已配置。 下一步是創建一個SLComposeViewController類的實例,並提供一個可選的完成處理程序,以便在用戶取消作曲者屏幕或用於發送消息時調用。接着,方法的範圍內可以被稱爲上的實例與該消息的內容,包括消息的初始文本,圖像附件和一個URL來初始化對象:

  • setInitialText: - 設置初始SLComposeViewController實例上的消息文本。
  • addImage: - 將圖像文件作爲附件添加到消息中。
  • addURL: - 向消息添加一個URL。該方法自動處理URL縮短。 以上每個方法都會返回一個布爾結果,指示添加的內容是否成功。父視圖控制器的方法:

    SLComposeViewController *composeController = [SLComposeViewController 
           composeViewControllerForServiceType:SLServiceTypeTwitter]; 
    
        [composeController setInitialText:@"Just found this great website"]; 
        [composeController addImage:postImage.image]; 
        [composeController addURL: [NSURL URLWithString: 
           @"http://www.website.com"]]; 
    
    [self presentViewController:composeController 
          animated:YES completion:nil]; 
    

    一旦調用,該方法

最後,當消息準備好被呈現給用戶,所述SLComposeViewController對象模態通過調用presentViewController呈現將向作爲用戶提供的作曲者視圖提供通過方法調用預先配置的任何文本,圖像和URL內容。一旦顯示,用戶可以選擇修改消息文本,取消消息,添加位置數據或發送消息。如果完成處理程序已配置,它將被調用並傳遞一個值,指示用戶在作曲者視圖中執行的操作。可能的值爲:

  • SLComposeViewControllerResultCancelled - 用戶通過觸摸取消按鈕取消了構圖會話。
  • SLComposeViewControllerResultDone - 用戶通過觸摸發送按鈕發送編寫的消息。
相關問題