Ben
Ben

Reputation: 1031

IOS5 Spanish character encoding

I have a form with UItextfield and when entering this characters ( á é í ó ú ü ñ ) and send to a server via POST method, the content of this characters are transformed.

eg.: this word "ñiña" wrote in the Textfield become on output as "√±i√±a"

 NSString *post = [NSString stringWithFormat:@"&email=%@&password=%@&id_user=%@&iso_pais=%@&lugar=%@&titulo=%@&comment=%@&api_key=3333333",emailstring,passstring,idstring,locatedAtisocountry,cityLabel.text,title.text,description.text];

                NSLog(@"%@",post);
                NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
                NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
                NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.myweb.com/api/exp?format=json"]]];
                [request setHTTPMethod:@"POST"];

The output of the post method is :

&email=username&password=5555&id_user=1032&iso_pais=PL&lugar=Madrid&titulo=ñiña&comment=&api_key=3333333

But the server side decodes the content of the key "titulo" as "nina" and should be "ñiña"

How can I fix this issue with this characters ?

Upvotes: 0

Views: 623

Answers (2)

AliSoftware
AliSoftware

Reputation: 32681

You should use percent escapes when passing characters thru the GET or POST parameters.

Use the stringByAddingPercentEscapesUsingEncoding: method for that.

Also don't use NSASCIIStringEncoding but rather NSUTF8StringEncoding if your server supports UTF8 (hopefully it does) and don't allow lossy convertion (that's the part in your code that transforms "ñ" to "n")

Upvotes: 1

Walter
Walter

Reputation: 5887

I think it is safer to use NSUTF8StringEncoding when you are trying to retain accent marks instead of NSASCIIStringEncoding.

You might need to update your server encoding since the two need to match.

Upvotes: 1

Related Questions