IOS postal code for Hebrew characters

I need to make an HTTP message from my iPhone application into a google document. It works great for English, but Hebrew works like everyone ???????? in google doc.

This is what I do:

NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 

What am I missing?

EDIT: I tried the proposed solution here and looked at the ahmedalkaf link here , but no luck.

+6
source share
4 answers

Option 1

You set your Content-Type to application/x-www-form-urlencoded , but you did not have the url of your content.

The URL encoding of your NSString message and changing the encoding to UTF-8 (I cannot tell you why, but it is necessary for it to 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 remain unchanged. However, you should consider using an asynchronous request. When using a synchronous request, your user interface will stop responding until the request is complete. This does not happen with asynchronous request.

Option 2

For HTTP requests, I usually use the ASIHTTPRequest library: http://allseeing-i.com/ASIHTTPRequest/

It takes several minutes to integrate into your project, but everything is simplified. You do not need to bother 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]; 

What is it.

To configure ASIHTTPRequest, follow these instructions http://allseeing-i.com/ASIHTTPRequest/Setup-instructions and do not forget to add the compiler flag -fno-objc-arc to the library files in the "Project Settings" section โ†’ Phase Assembly โ†’ Compile Sources, if you use ARC.

+2
source

(This basically explains why UTF-8 encoding is needed for ASCII. I will not explain part of the URL for this problem, as Tim Bodeit has already done well.)

Main Explanation:

ASCII encoding only contains characters with a character without a character, while UTF-8 contains characters for most languages.

More details:

ASCII only has the characters shown in the image below, although it only fits in seven bits (or eight, depending on how 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 for Unicode, because when your application uses characters that are in ASCII (in English), it will be only one byte. When your application uses Hebrew, the string will be several bytes longer. If you use Unicode, your string will ALWAYS have more than one byte, whether in English or Hebrew.

Decision? Just change everything that ASCII says to UTF8.

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

This is Joel Spolsky (co-founder of Stack Exchange), and it is very useful for any program that uses strings that are most programs. You will especially benefit if you work with Hebrew and English.

By the way, you should remove the tags for iOS, iPhone, iPad and replace it with Objective-C, since your code fragment can also be applied to programming for Mac.

+3
source

For Hebrew characters, NSUTF8StringEncoding is used to publish data to the server.

 NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil]; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
+1
source

You can use AFNetworking , it will send your data as utf-8 encoding, I use this library for my projects in Spanish and always get the correct character for:

 NSString *encodeUrl = [@"document/d/Hphb4NsfxZLxTl7p3LMCWCpm9MCWskgoTFWzhP1Fpqap/edit" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; //setup the post using AFHTTPClient AFHTTPClient *afHttpRequest = [[[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://docs.google.com"]] autorelease]; NSURLRequest *request = [afHttpRequest requestWithMethod:@"POST" path:encodeUrl parameters:post]; //loading indicator [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount]; [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){ //some response } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){ //the request fails } ]; //fire the request [operation start]; 

What is [Util append]? add additional fields to fields? or just add

+1
source

Source: https://habr.com/ru/post/952272/


All Articles