博客> UIWebView与WKWebView 无法加载doc文件,和加载text乱码
UIWebView与WKWebView 无法加载doc文件,和加载text乱码
2017-08-14 05:54 评论:0 阅读:118 西单_夜未央
UIWebView与WKWebView 无法加载doc文件 和加载text乱码 计算缓存 清流缓存

大家新年好!新的一年,祝大家:事事如意,工资翻倍,更上一层楼!

今天主要介绍两个BUG:

A:UIWebView与WKWebView 加载text乱码

原因: 因为在UIWebView与WKWebView中加载text,一般编码为 NSUTF8StringEncoding,有时候因为加载html的text,编码格式就会有所不同,从而会导致一些乱码

解决方法: 转化编码:GBK解码或者GB18030解码

代码:

  NSString *body = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    if (!body)
    {
        //如果之前不能解码,现在使用GBK解码
        NSLog(@"GBK");
        body = [NSString stringWithContentsOfURL:url encoding:0x80000632 error:nil];
    }
    if (!body) {
        //再使用GB18030解码
        NSLog(@"GBK18030");
        body = [NSString stringWithContentsOfURL:url encoding:0x80000631 error:nil];
    }

B:UIWebView与WKWebView 无法加载doc文件

原因: doc是word2003及之前的格式,现在一般为docx格式,而UIWebView与WKWebView只能加载docxgeshi (今天我也纠结了好久)

解决方法: 把doc格式转化成docx格式

代码: 原理就是把doc格式的文件,写入到本地文件,修改本地文件的后缀

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *urlStringLastComponet = [urlString lastPathComponent];

//    NSLog(@"urlStringLastComponet = %@",urlStringLastComponet);

NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

filePath = [filePath stringByAppendingPathComponent : @"attachment" ]; //拼接路径 temp/attachment
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; //创建 文件夹

NSRange range = [self.tittlStr rangeOfString:@"."];

NSString *suffix = [self.tittlStr substringFromIndex:range.location];

//    NSLog(@"文件格式:%@",suffix);

if ([suffix isEqualToString:@".doc"]) {

    suffix = @".docx";
}

filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",urlStringLastComponet,suffix]];

self.filePath = filePath;

if ([fileManager fileExistsAtPath:filePath]) {

    return YES;
}else{

    [fileManager createFileAtPath:filePath contents:nil attributes:nil];  //创建  文件
    return NO;
}

下面是我计算缓存和清理缓存的方法:

//计算缓存
- (float )folderSizeAtPath:(NSString*) folderPath{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [[paths lastObject] stringByAppendingPathComponent:@"attachment"];

NSFileManager* manager = [NSFileManager defaultManager];

if (![manager fileExistsAtPath:path]) return 0;

NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];

NSString* fileName;

long long folderSize = 0;

while ((fileName = [childFilesEnumerator nextObject]) != nil){

    NSString* fileAbsolutePath = [path stringByAppendingPathComponent:fileName];

    folderSize += [self fileSizeAtPath:fileAbsolutePath];

}

return folderSize/(1024.0*1024.0);

}

//清理缓存
- (void)clearCache:(NSString *)filePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [[paths lastObject] stringByAppendingPathComponent:@"attachment"];

    NSLog(@"%@",path);

    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];

    for (NSString *p in files) {

        NSError *error;

        NSString *Path = [path stringByAppendingPathComponent:p];

        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {

            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];

        }
    }
}
收藏
0
sina weixin mail 回到顶部