Reputation: 243
I have an iOS 5 application that allows users to enter username with international keyboards. I want to check if the input (NSString *) contains an emoji character or not.
Disabling emoji keyboard is not an option (Using UIKeyboardTypeASCIICapable as it disables some of the international keyboards).
I tried this. But it does not detect some of the characters like these.
Is there a good way to solve this problem?
Upvotes: 5
Views: 5614
Reputation: 307
To iOS 10 and below, you should check if the unicode if between the following values.
if ((0x1d000 <= uc && uc <= 0x1f77f) || (0x1f900 <= 0x1f9ff)) {
//is emoji
}
Upvotes: 0
Reputation: 2186
Use https://github.com/woxtu/NSString-RemoveEmoji Category, but note that iOS 9.1 added more emojis that above mentioned method doesnt recognize (especially these ones:🤐🤑🤒🤓🤔🤕🤖🤗🤘🦀🦁🦂🦃🦄🧀).
FIX: replace
return (0x1d000 <= codepoint && codepoint <= 0x1f77f);
in isEmoji
method with
return (0x1d000 <= codepoint && codepoint <= 0x1f77f) || (0x1F900 <= codepoint && codepoint <=0x1f9ff);
Upvotes: 1
Reputation: 243
I was able to detect all emojis in iOS 5 and iOS 6 emoji keyboards using following method https://gist.github.com/4146056
Upvotes: 10
Reputation: 3260
NSData *data = [yourstringofemo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([goodValue rangeOfString:@"\u"].location == NSNotFound)
{
goodvalue string contains emoji characters
}
else
{
goodvalue string does not contain emoji characters
}
Upvotes: -1