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 ?
You should use percent escapes when passing characters thru the GET or POST parameters.
Use the
stringByAddingPercentEscapesUsingEncoding:method for that.Also don’t use
NSASCIIStringEncodingbut ratherNSUTF8StringEncodingif your server supports UTF8 (hopefully it does) and don’t allow lossy convertion (that’s the part in your code that transforms “ñ” to “n”)