不知道大家有没有遇到这样的情况,下载的文件文件名为中文,而iOS下载到本地,取response
的suggestedFilename
,但得到的却是é��åº�å¸�工伤æ�¥é��ä½�é�¢ä¼�é£�è¡¥å�©è´¹ã��交é��é£�å®¿è´¹å®¡æ ¸ç»�ç®�表.xls
这样的乱码,WTF?而且使用UIDocumentInteractionController
控件也无法预览(无法预览与名称编码有关,无法创建相应的路径)。
解决方案
自定义文件名
自己自定义文件名,比如20200909120808
这种以时间戳当作文件名的。
编码解决
上面的方案的确可以解决问题,但是如果我就是想拿到文件本来的名字,比如文件是什么密钥,你随便起个名字,打开是一串数字,根本不知道是什么东西。
那么,我们需要知道究竟是什么原因造成了乱码,查了很久的资料,终于发现原来iOS,甚至MacOS上 下载文件,文件名是ISO-8859-1编码
!!!!!
既然知道了是ISO编码这个关键信息,那就好办了,我们把它转换回来就行了,转换方法如下:
+ (NSString *)_859ToUTF8:(NSString *)oldStr
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin1);
return [NSString stringWithUTF8String:[oldStr cStringUsingEncoding:enc]];
}
通过上图,我们可以看到,确实是转换过来了。
那直接把转换后的字符串,当作文件名,我们发现,文件保存不了!! why ? 买了否冷?
那肯定还是因为中文字符,我们做一下处理,如下代码为YYKit中NSString的分类中的方法
- (NSString *)stringByURLEncode {
if ([self respondsToSelector:@selector(stringByAddingPercentEncodingWithAllowedCharacters:)]) {
/**
AFNetworking/AFURLRequestSerialization.m
Returns a percent-escaped string following RFC 3986 for a query string key or value.
RFC 3986 states that the following characters are "reserved" characters.
- General Delimiters: ":", "#", "[", "]", "@", "?", "/"
- Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
should be percent-escaped in the query string.
- parameter string: The string to be percent-escaped.
- returns: The percent-escaped string.
*/
static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
[allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
static NSUInteger const batchSize = 50;
NSUInteger index = 0;
NSMutableString *escaped = @"".mutableCopy;
while (index < self.length) {
NSUInteger length = MIN(self.length - index, batchSize);
NSRange range = NSMakeRange(index, length);
// To avoid breaking up character sequences such as 👴🏻👮🏽
range = [self rangeOfComposedCharacterSequencesForRange:range];
NSString *substring = [self substringWithRange:range];
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
[escaped appendString:encoded];
index += range.length;
}
return escaped;
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
NSString *encoded = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(__bridge CFStringRef)self,
NULL,
CFSTR("!#$&'()*+,/:;=?@[]"),
cfEncoding);
return encoded;
#pragma clang diagnostic pop
}
}
调用方法后,便可正常的存储和预览分享了。
在此记录一下。