iOS: How to avoid special characters being entered in a search bar? - ios

I may be looking at this incorrectly, but right now, if a user enters certain special characters in a search bar, the app will crash. These include \ and '.
The apostrophe is a problem, in that words like "isn't" and "doesn't" will cause a crash
How should I go about working around this problem?
Many thanks

Before doing a search using the search string, pre-filter it, like this:
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:#""];

Why don't you escape the characters and actually search for what the user wants instead of ignoring or removing what they want ?
Are you using SQLite ? If so then you can escape a single quote by another single quote.

The UISearchBar utilizes the UISearchBarDelegate protocol. To stop users entering \ and ' all you need to do is to override the searchBar:shouldChangeTextInRange:replacementText: method and if you sense the special characters in the text, you simply return NO.
But handling (rather than ignoring) these special chars in your program is a better user experience. After all why are you users even searching for "isn't" and "doesn't" in the first place?

Related

Char difference between UISearchBar and UITextField

I'm using AFNetWorking POST request to send strings typed by users to my server.
Depending on where the string is caught from, the chars used don't seam to be the same.
For exemple, the simple quote EDIT: apostrophe used by a UITextField is
'
and the one used by UISearchBar is
’
As result, my server can utf8_decode the first one, but not the second one. So I've temporary fixed this issue using stringByReplacingOccurrencesOfString:withString:, but I can't ensure that some other chars can be replaced by strange ones in the UISearchBar.
Is their any settings to apply to the UISearchBar to force it to use the same chars as the UITextField.
Alternatively, maybe is there a method to apply to the string caught from the UISearchBar to use the correct chars ?

UIActivityViewController Gmail share extension duplicated body

I've seen some weird thing with the gmail share extension, like the body and the subject fields are empty if you want to share a content containing a & character.
It's possible to get it working with escaping the & character with &, however this way the body in the message is duplicated (once the original, and once the escaped.
Do you guys know a workaround for this issue?
Thanks in advance!
The only workaround I've found is to remove every special characters (I was able to replace & with 'and' - it was in text, not in link)

Sending \t in NSString IOS

I am currently working on an app that connects to custom server using NSStream. Once connected the app allows for user input via a textfield. In order for the server to recognize an incoming command a \t needs to be first and then the string from something like self.inputField.text.
I have everything working up to this point as far as opening/closing of sockets, sending/receiving etc.
The problem is my sent string looks like so: \tSOMECOMMAND but the \t is not being interpreted as a tab but as string '\tSOMECOMMAND'. How can I prepend a \t (tab) to the text in my input field?
As always thanks in advance!
You shouldn't require the user to add the tab character as it's counter-intuitive, easy to forget and an implementation detail they don't need to know about.
In your controller object:
Get the string from the text field.
Prepend the tab character.
Send the string to the server.
A user typing a backslash and a 't' is not necessarily the same as an escape sequence for a tab.
Those will be sent as two literal characters.
Your code needs to identify that escape sequence string and replace the string with a tab character. That's what the compiler does to an escape sequence when it is found within delimiters within which it expects there might be an escape sequence.

Escape bad characters for iOS UILabel?

I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!
Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.

Formatting NSString for superscript and subscript

I am writing a utility app for some coworkers. The app is essentially a custom notepad, with buttons that represent the shorthand they use to transcribe a task. All of the buttons add a string to arrays that I have set up to hold the transcript, and I add the strings to the row arrays like this.
[currentRow addObject:#"("];
Some of the shorthand needs to be written in subscript, and some in superscript. There are not Unicode characters for all of the characters that I need, so I have been trying to sort through the code around Attributed Strings,but I'm not quite getting it. Does anyone have advice on this or some sample code?
Also, after this transcript is printed to the screen during transcription, I send it to an email message body.. so I assume I'll need to worry about formatting there as well. I am currently using plain text, but the email could be HTML. Thanks!
If you display the text in a WebView you can use html tags to set superscript. It also has the advantage to run on older iOS versions and you can reuse the text in your mail.
NSString *myText=#"This text contains <sub>subscript</sub> and <sup>superscript</sup> text.";
[self.myWebView loadHTMLString:myText baseURL:nil];

Resources