美文网首页
iOS URL 含有中文 含有乱码

iOS URL 含有中文 含有乱码

作者: 唐人街的乞丐 | 来源:发表于2023-01-15 09:55 被阅读0次

1.URL 含有中文

用sdwebimage 加载图片加载不出来,打印发现URL中有中文

解决方案

url 转换一下
+(NSString *)stringChangeChineseTransformWithUrlString:(NSString*)string{
    return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#^{}\"[]|\\<> "].invertedSet];
}

2.URL 含有乱码

http://xxx.xxx.xxx.com?name=E5%BC%A0%E7%91%B6
需求是拿到name后面的名称,用 utf8不好用
解决方案

+(NSString *)StringDecodeChangeForChineseWithString:(NSString *)str{
    NSMutableString *resultStr = [NSMutableString stringWithString:str];
    [resultStr replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0,[resultStr length])];
    return [resultStr stringByRemovingPercentEncoding];;
}

相关文章

网友评论

      本文标题:iOS URL 含有中文 含有乱码

      本文链接:https://www.haomeiwen.com/subject/rixacdtx.html