views:

40

answers:

1

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.

+1  A: 

What if you change it to:

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

documentation link

Robot K
Thanks Robot K! That worked!I guess I really need to search the documentation more for all the possible encodings. I didn't see that one mentioned where I looked!
It's not easy to find. NSString is a huge class, and some of the methods names are rather confusing.
Robot K