在發(fā)送網(wǎng)絡請求時,由于用戶輸入了emoji表情,服務端返回錯誤提示信息。為了解決服務器不能驗證emoji編碼的問題,需要在本地進行emoji的輸入控制(正常情況下應該由服務器在數(shù)據(jù)庫中添加emoji對應的轉(zhuǎn)碼表以支持客戶端發(fā)送emoji表情),在網(wǎng)上搜了下iOS端解決辦法如下:
1.當用戶切換鍵盤為Emoji表情時,輸入的表情不響應(即表情符號不顯示到UITextView或UITextField)。這里可以通過UITextView或UITextField的回調(diào)和是否為emoji鍵盤:
[[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"]
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//不支持系統(tǒng)表情的輸入
if ([[[UITextInputMode currentInputMode ]primaryLanguage] isEqualToString:@"emoji"]) {
return NO;
}
return YES;
}
2.通過過濾用戶輸入的emoji來實現(xiàn)
//過濾表情
+ (NSString *)filterEmoji:(NSString *)string {
NSUInteger len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
const char *utf8 = [string UTF8String];
char *newUTF8 = malloc( sizeof(char) * len );
int j = 0;
//0xF0(4) 0xE2(3) 0xE3(3) 0xC2(2) 0x30---0x39(4)
for ( int i = 0; i < len; i++ ) {
unsigned int c = utf8;
BOOL isControlChar = NO;
if ( c == 4294967280 ||
c == 4294967089 ||
c == 4294967090 ||
c == 4294967091 ||
c == 4294967092 ||
c == 4294967093 ||
c == 4294967094 ||
c == 4294967095 ||
c == 4294967096 ||
c == 4294967097 ||
c == 4294967088 ) {
i = i + 3;
isControlChar = YES;
}
if ( c == 4294967266 || c == 4294967267 ) {
i = i + 2;
isControlChar = YES;
}
if ( c == 4294967234 ) {
i = i + 1;
isControlChar = YES;
}
if ( !isControlChar ) {
newUTF8[j] = utf8;
j++;
}
}
newUTF8[j] = '\0';
NSString *encrypted = [NSString stringWithCString:(const char*)newUTF8
encoding:NSUTF8StringEncoding];
free( newUTF8 );
return encrypted;
}
3.通過判斷用戶輸入的字符串時候含有表情來進行對應的操作
+ (NSString *)filterEmoji:(NSString *)string {
NSUInteger len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
const char *utf8 = [string UTF8String];
char *newUTF8 = malloc( sizeof(char) * len );
int j = 0;
//0xF0(4) 0xE2(3) 0xE3(3) 0xC2(2) 0x30---0x39(4)
for ( int i = 0; i < len; i++ ) {
unsigned int c = utf8;
BOOL isControlChar = NO;
if ( c == 4294967280 ||
c == 4294967089 ||
c == 4294967090 ||
c == 4294967091 ||
c == 4294967092 ||
c == 4294967093 ||
c == 4294967094 ||
c == 4294967095 ||
c == 4294967096 ||
c == 4294967097 ||
c == 4294967088 ) {
i = i + 3;
isControlChar = YES;
}
if ( c == 4294967266 || c == 4294967267 ) {
i = i + 2;
isControlChar = YES;
}
if ( c == 4294967234 ) {
i = i + 1;
isControlChar = YES;
}
if ( !isControlChar ) {
newUTF8[j] = utf8;
j++;
}
}
newUTF8[j] = '\0';
NSString *encrypted = [NSString stringWithCString:(const char*)newUTF8
encoding:NSUTF8StringEncoding];
free( newUTF8 );
return encrypted;
}
- emoji在NSUTF8StringEncoding編碼下占用4個字節(jié),中文編碼占用3個字節(jié),可以根據(jù)- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string方法,把string做處理,依據(jù)
NSUInteger stringUtf8Length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if(stringUtf8Length >= 4 && (stringUtf8Length / string.length != 3)) {
return NO;
}
判斷,可排出大多數(shù)表情符號,還有少量的符號排除不了,比如_ ,占3個字符。
代碼不實用在九宮格鍵盤上的,可以在輸入時打斷點查看內(nèi)容或直接輸出%x查看。