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.
What if you change it to:
encodedSelectedWord = [selectedWord stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", encodedSelectedWord];
documentation link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With