微信开放平台 - 微信昵称中的 Emoji 字符在授权后的 iOS 应用中显示乱码
黄舟
黄舟 2017-04-17 13:46:09
0
3
839

公司的一款 iOS 应用能够使用微信授权认证登录注册, 然后把微信的用户昵称设置为注册用户的默认昵称.

目前遇到的问题是, 在微信那边用户名可以插入 Emoji 表情, 可是那些表情到我们应用这边就全部乱码了.

现在想知道有什么方法可以使微信那边的昵称可以完整的显示在这边 iOS 应用上, 不打算过滤掉那些 Emoji 字符.

原来的昵称是:

显示结果是:

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
巴扎黑

The current answer is that Emoji support has not been added to the database, causing the nicknames with Emoji expressions obtained from WeChat to be garbled. There are two solutions:

1. Add Emoji support to the database, there is a solution on SO
2. Filter out the garbled characters obtained when the user registers, leaving only normal characters. I used the following filtering method. The code is relatively simple, but it can be used normally:

- (NSString*)removeEmoji:(NSString *)username {

    NSString *regex = @"^[a-zA-Z0-9_\u4e00-\u9fa5]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSString *temp = nil;

    for(int i = 0; i < [username length]; i++)
    {
        temp = [username substringWithRange:NSMakeRange(i, 1)];
        if ([predicate evaluateWithObject:temp]) {
            NSLog(@"%@", temp);
            NSLog(@"This character is OK");
        } else {
            NSRange range = NSMakeRange(i, 1);
            username = [username stringByReplacingCharactersInRange:range withString:@" "];
        }
    }

    NSString *withoutEmojiUsername = [username stringByReplacingOccurrencesOfString:@" " withString:@""];

    return withoutEmojiUsername;
}
Peter_Zhu

I have never written an iOS program. My personal opinion is that WeChat also uses characters to store this kind of nicknames with emoticons, and may just happen to be this character When it comes to the iOS system, it becomes another emoticon.
Wait for the master.

Peter_Zhu

Check whether the encoding of the string is normal, or there may be a problem with the string format.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template