2014-02-06 44 views
0

我嘗試使用下面的代碼爲iOS如何在twitter上發佈私人推文給ios中的朋友/關注者?

-(void)postMessageToFriend:(NSString *)ID withImage:(UIImage *)image{ 

    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 

     if (granted && !error) { 

      NSArray *accountsList = [accountStore accountsWithAccountType:accountType]; 

      int NoOfAccounts = [accountsList count]; 
      if (NoOfAccounts >0) { 

       NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/show.json"]; 
       ACAccount *twitterAccount = [accountsList lastObject]; 

       NSDictionary *p [email protected]{@"status":@"post successfully.....",@"user_id":ID}; 
       SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:p]; 
       NSData *imageData = UIImageJPEGRepresentation(image,0.5); 
       [postRequest addMultipartData:imageData withName:@"media[]" type:@"image/jpeg" filename:@"image.jpg"]; 

       [postRequest setAccount:twitterAccount]; 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResposnse, NSError *error){ 
        NSError *jsonError = nil; 
        NSDictionary *friendsdata = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError]; 
        NSLog(@"response value is: %@ %d ",friendsdata,[urlResposnse statusCode]); 
       }]; 
      } 
     } 
    }]; 
} 

此處發佈消息給好友ID是朋友的Twitter ID

而我發佈的tweet 2個朋友得到錯誤,如下面

errors =  (
       { 
      code = 189; 
      message = "Error creating status."; 
     } 
    ); 

請幫助爲什麼可以推特給特定的朋友/追隨者在IOS ..

+0

嘗試用這個網址'的https:// api.twitter.com/1.1/direct_messages/new.format'或'https://api.twitter.com/1.1/direct_messages/new.json' –

回答

0

使用下面的鏈接我解決了我的問題

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"]; 
0

最後我得到了一個解決方案。請嘗試以下我的代碼:

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 

    if (granted && !error) { 


     NSArray *accountsList = [accountStore accountsWithAccountType:accountType]; 

     int NoOfAccounts = [accountsList count]; 
     if (NoOfAccounts >0) { 

      NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/direct_messages/new.json"]; 
      ACAccount *twitterAccount = [accountsList lastObject]; 


      NSDictionary *param [email protected]{@"user_id":[NSString stringWithFormat:@"%@",userid],@"text":@"post successfully....."}; 
      SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:param]; 
      [postRequest setAccount:twitterAccount]; 
      [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResposnse, NSError *error){ 
       NSError *jsonError = nil; 

       NSLog(@"response value is: %@ %d ",friendsdata,[urlResposnse statusCode]); 
           }]; 
     } 
    } 
}]; 
相關問題