0

I have a search field, which is a UITextView, where the user enters data that will be searched in a MySQL database in a server.

The search and PHP that Xcode calls work for normal characters, but the encoding I am doing does not seem to work correctly for special characters such as ā, as an example. I have managed to save data with special characters to the MySQL database, but for some reason the code I am doing to search does not seem to encode the data correctly.

the current code I have is:

NSString *searchString;
searchString = [searchTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
searchString = [searchString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

searchTextField.text is a UITextView. Any ideas why this is not working?

OK - I have solved the problem BUT I don't understand why. So would be grateful for explanations.

The way I have solved the problem, which by the way seems to be like using a hammer to crack a nut, is by saving the string I am searching in a SQLite database. Once saved, it is assigned to searchString and it gets encoded properly.

Can someone explain why this is the case?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
user3079872
  • 191
  • 1
  • 5
  • 15
  • Is this search string something you just add to some HTTP request that you're building manually? If that's the case, yes, you have to percent escape it so that the web server will parse the `x-www-form-urlencoded` request properly. Unfortunately, `stringByAddingPercentEscapesUsingEncoding` is not adequate, letting some key characters go unescaped. But before we dive into that, please confirm how you're using this `searchString` and how you're building the request (and how your web service is taking this request and querying the database). – Rob Oct 22 '15 at 14:46
  • Frankly, it's also unclear as to what the "solution" was and how its use of the search string differs from the direct use of the string. Perhaps you can share [the smallest possible code snippets](http://stackoverflow.com/help/mcve) that illustrate (a) the original code that wasn't working; and (b) the fix (even if suboptimal). – Rob Oct 22 '15 at 14:56
  • @Rob thanks for the question. I have most probably not explained fully what I was trying to do. The UITextView is "sent", supposedly encoded, within a URL string. So, if the searching string did not contain any special characters, the URL would work: i.e. a list of matching strings would be returned. However, if the string contained special characters, and I did the above encoding to it, it would not work. IF I "saved" the search string to a sqlite database on the phone, then, via a SELECT statement assigned it to an NSString, and then did the above encoding, the search would work. – user3079872 Oct 22 '15 at 15:40
  • Understood, but the problem could be either how you create the request or how you create the query. We need more information to answer your question. But, at the very least, if you're percent escaping, the only reason you would do that is that you're manually building the request (rather than using something like AFNetworking, which takes care of this for you), you can't just use `stringByAddingPercentEscapesUsingEncoding`, but rather something like http://stackoverflow.com/a/20777164/1271826. But for the broader question, we need a [MCVE](http://stackoverflow.com/help/mcve). – Rob Oct 22 '15 at 16:52

0 Answers0