iOS HTTP Post encoding for Hebrew characters

Option 1

You have set your Content-Type to application/x-www-form-urlencoded, but you haven't url-encoded your contents.

URL-encoding your post NSString and changing the Encoding to UTF-8 (I cant tell you why, but it's needed to make it work) should do the job.

NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil];
post =[post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

Everything else can stay the same. However you should think about using an asynchronous request. When using a synchronous request, your user interface will become unresponsive, until the request is finished. This doesn't happen with an asynchronous request.

Option 2

For HTTP-Requests I usually use the ASIHTTPRequest Library: http://allseeing-i.com/ASIHTTPRequest/

It takes a couple of minutes to integrate into your project, but it makes everything way more simple. You don't have to mess around with encodings etc.

NSURL *url =[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request addPostValue:self.firstName.text forKey:@"entry.xxxx"];
[request addPostValue:self.phone.text forKey:@"entry.yyyy"];
[request addPostValue:self.email.text forKey:@"entry.zzzz"];

[request startAsynchronous];

That's it.

To set up ASIHTTPRequest follow these instructions http://allseeing-i.com/ASIHTTPRequest/Setup-instructions and remember to add a -fno-objc-arc compiler flag to the libraries files under Project Settings -> Build Phases -> Compile Sources if you are using ARC.


(This mainly explains why UTF-8 encoding is needed over ASCII. I'm not going to explain about the URL-encoding part of this problem, as Tim Bodeit already did nicely.)

Basic Explanation:

ASCII encoding only contains English, unaccented characters, while UTF-8 contains characters for most languages.

Details:

ASCII has only the characters shown from the image below, although it fits into only seven bits (or eight, depending on which way you look at it).

ASCII Characters and how they are encoded. From the blog post by Joel Spolsky in the link below

However, both Unicode and UTF-8 can contain any characters from almost all major languages. UTF-8 is preferred over Unicode, because when your app is using characters that are in ASCII (English), it will be only one byte. When your app is using Hebrew, the string will be a few bytes longer. If you use Unicode, your string will ALWAYS have more than one byte, whether its in English or in Hebrew.

The solution? Just change everything that says ASCII to UTF8.

You (and every other programmer) should absolutely read this article.

It's by Joel Spolsky (co-founder of Stack Exchange), and it's very beneficial for any program using strings, which is most programs. You especially will benefit if you are doing work using both Hebrew and English.

By the way, you should remove the tags iOS, iPhone, iPad, and replace it with Objective-C, as your code snippet can apply to programming for Macs, too.