I just want to see whether there is an established way to do this, or how one would go about this.
I have a text field that essentially acts as a form in my iOs app where a user can post something. I can't have users posting swear words/inappropriate crap, so I want to filter out and display an error if the string they enter contains one of these words.
How do other apps in Swift do this? Do they just search through the string to see if it contains the word (obviously not within other words, but standing alone) or is there another method?
How can I accurately filter out the swear words from my user post in Swift?
Construct a list of words you consider to be swear words, and simply check the user entered string whether any of these words are contained within the string.
Swift 3:
Swift 2.2:
Extension checking for foul language.
Swift 4.2
Example Usage:
Extension:
I would suggest looking into an
API
to which you can submit a string and get aJSON
response containing information such as:I found a couple sources via Google. Check these out and do a little more research to find if an
API
is the best fit for you and which one you should use. I would assume that using anAPI
like the one I have listed below would be the most practical approach, as you would NOT have to compile a list of "bad" words yourself and use resources from the device to sort through the list (which can contain thousands of words).Rather, you can simply submit a string using the
API
to get a network response containing the data in JSON format from theAPI
server.Why not let the API Server do the logic for you and just spit out an answer?
NeutrinoAPI
I created a class that enables you to feed a string in and remove profanity. Here's a link to the repo.
Here's the code:
If this method returns a range,
an offensive word has been found. While this method can be used to remove the offending strings: