18

I don't want the Emoji keyboard allowance in my application so I want to disable it only in my application. There is one way to do it by applying the answer from this link:

Making An Emoji Enabeling App

But this would not work on iOS 5 (iOS 4.3 do work). Is there any way to disable Emoji keyboard in iOS 5. Thank you.

Community
  • 1
  • 1
Protocole
  • 1,733
  • 11
  • 28
  • 42

3 Answers3

39

You can simply set the property keyboardType of the UITextField or UITextView to UIKeyboardTypeASCIICapable. This disables the Emoji Keyboard for this UI element.

mschluepmann
  • 431
  • 4
  • 3
17

@mschluepmann, But set UIKeyboardTypeASCIICapable can not input Chinese

And you can do it like below

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (IS_OS_7_OR_LATER) {
        if ([textField isFirstResponder]) {
            if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { // In fact, in iOS7, '[[textField textInputMode] primaryLanguage]' is nil
                return NO;
            }
        }
    } else {
        if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"emoji"] ) {
            return NO;
        }
    }

    return YES;
}

But sometimes, the emoji may not entered by emoji keyboard. For example, when you type "哈哈" it shows emoji on the header of the keyboard. In the case, the code above will make no effect. So you should do a twice validation as following:

- (BOOL)isValidString
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:NSMatchingWithTransparentBounds range:NSMakeRange(0, [self length])];

    if (numberOfMatches > 0) {
        return NO;
    }

    return YES;
}
MeganZhou
  • 346
  • 3
  • 7
  • Excellent solution, this should be marked as the true answer. – Ser Pounce May 23 '14 at 22:26
  • the isValidString method doesn't work correctly. It also blocks special characters like german umlauts (ä, ü, ö) and so on... :( – Maniac Oct 07 '14 at 10:37
  • I tested this on iOS 7 & iOS 8 and it prevents Euro Symbol and french accented characters. This is a major blocker for my application and audience. – foob.ar Dec 03 '14 at 19:14
  • I've got the same problem. I've disabled the Emoji from the keyboard, but I saw some use did copy-past Emoji from another app (i.e: Mail). To prevent copy-past, I've found this nice extension of NSSString on Github: https://github.com/woxtu/NSString-RemoveEmoji Hope that's helps ;) – Lapinou Jan 08 '15 at 12:52
  • While blocking input based on the `primaryLanguage` of the current keyboard is a quick solution, it can cause some confusion since nothing will be able to be added to the text field while this check is in place and the emoji keyboard is the first responder. For example, pasting [valid] text while the emoji keyboard is open will not work. A far better solution is the category that @Lapinou posted a link to, since that checks directly against the unicode values for emoji. – timgcarlson Jul 14 '15 at 19:46
0

@Lapinou's answer helped me, repost his category of NSString on github: NSString-RemoveEmoji

Yahoho
  • 420
  • 4
  • 9