Stack Exchange
Getting %20 in twitter private message
Hi here is my code for sending private message to twitter follower/followings,message has been sent but problem is followers are getting message string with %20 
 -(void)PostOnTwitter :(NSArray *)ParamArray
{

 NSLog(@"ParamArray===%@",ParamArray);
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    if(!accountStore)
        accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
     {
         if(granted!=0)
         {
             NSArray *twitterAccounts = [self.accountStore accountsWithAccountType:accountType];

             NSURL *MsgUrl=[NSURL URLWithString:@"https://api.twitter.com/1.1/direct_messages/new.json"];


             NSDictionary *Msgparams = @{@"screen_name" : [ParamArray objectAtIndex:0], @"text" :
                                      [[ParamArray objectAtIndex:1]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]  };
         SLRequest *Msgrequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:MsgUrl parameters:Msgparams];
             [Msgrequest setAccount:[twitterAccounts lastObject]];
             [Msgrequest performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error) {
              if (responseData) {

                     if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
                         NSError *jsonError;
                         NSDictionary *timelineData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];

                    if (timelineData) {

                             [self performSelectorOnMainThread:@selector(CardHasSent) withObject:nil waitUntilDone:NO];
                         } 
                     } 
                 }

             }];

         }
     }];
  }
}

My followers are getting private message like

   user%20has%20sent%20you%20a%20card%20from%20happy%20heART%20cards.%20http://www.happyheartcards.net/o.php?t=ODYw


How can I send proper text?Please help. 
I think probelm is stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding,but if I don't use NSUTF8StringEncoding message sending fails.
ParamArray has not been encoded earlier ParamArray===(
    jry088,
    "user has sent you a card from happy heART cards. http://www.happyheartcards.net/o.php?t=OTA1"
)

%20 is indeed the string escape for a space character, and this is the correct behaviour that you are requesting in using stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding. However, the Twitter API does want POST data for direct messages encoded like this:
https://dev.twitter.com/docs/api/1.1/post/direct_messages/new
I thought the most likely thing therefore was that you were running the encoding twice, which would mean that your % in %20s are getting re-encoded to say "Hey, I really mean a percentage sign here. 
When I look at the code, there are multiple redundant calls to [ParamArray objectAtIndex:1] and earlier re-encoding of the string data into UTF8:
             NSString *StringData = [ParamArray objectAtIndex:1];

         const char *cString = [StringData UTF8String];

         NSString *Datastringone = [NSString stringWithFormat:@"%s",cString];

         NSString *str  = [ParamArray objectAtIndex:1];

None of these variables or this processing ever gets used. This made me wonder whether the string in the ParamArray has already been encoded into UTF8 in an earlier part of your application, so that you are encoding it for the second time when you pass it in here.
         NSDictionary *Msgparams = @{@"screen_name" : [ParamArray objectAtIndex:0], @"text" :
                                  [[ParamArray objectAtIndex:1]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]  };

EDIT
Actually, I'm going to return to what I originally posted, before I updated the above. I think you're actually getting exactly what you ask for with that encoded text, and the question shouldn't be why you're getting %20s in your text, but rather why the code you've got now is accepting a DM whereas it was failing after all.
If you look at the API reference I linked above (https://dev.twitter.com/docs/api/1.1/post/direct_messages/new) it shows the POST data as % encoded earlier on, but if you actually look at the full code example of what is to be submitted, the string isn't actually encoded at all:
94    "text": "hello, tworld. welcome to 1.1."

So first—change as little else as possible, but remove the encoding stage, and confirm that you still can't send a DM. It could be that you fixed something else without realising it and that is why the POST started working. This would obviously be helped by not embedding the encoding stage into the creation of the dictionary. You get much more readable code if you create all the values separately and then pass the final encoded and prepared variables in when you create the dictionary. A few more temporary variables or pointers may be created but that's well worth it for the increased readability of the code. :)

Yes Duncan you are right I have tried to send message without stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding but failed, got many links of stack overflow used NSUTF8StringEncoding.Please help with the correct encoding

I have removed all redundant codes,ParamArray has not been encoded earlier it containsParamArray===(
jry088,
"user has sent you a card from happy heART cards. http://www.happyheartcards.net/o.php?t=OTA1"
)
