Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Cyrillic characters as a URL parameter

I need to pass Cyrillic characters as a parameter in a URL in my iPhone app. A sample URL looks like:

http://www.mysite.com/script.php?message=страшная

When I use this URL in my browser, it returns the correct result. However, in my app, the cyrillic is not liked, and I end up getting a "bad url" in the didFailWithError code.

I have tried several encodings in my browser to get around this, but with no luck. Is there a way to actually send the exact URL example above via an iPhone app? The snippet of code I am using is:

selectedWord = @"страшная";
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", selectedWord];
NSLog (requestURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

In the above code, I have hardcoded the cyrillic word in the NSString *selectedWord for debug purposes. In the real application it will be dynamic based on user input.

I have tried a more complex POST, thinking that I could pass the cyrillic as data rather than text, but either I am doing it wrong, or it doesn't work either.

Any example regarding encoding I can try in my browser, then implement in the app would be helpful. I realize that the cyrillic is being rejected in the NSURL, but I was hoping there was a way around it with different code or encoding.

like image 689
user472938 Avatar asked Dec 07 '22 01:12

user472938


1 Answers

What if you change it to:

encodedSelectedWord = [selectedWord stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", encodedSelectedWord];

documentation link

like image 130
Kris Markel Avatar answered Dec 17 '22 13:12

Kris Markel