Objective-C

NSURL的fileURLWithPath:自动编码问题

简介:此问题导致在使用WKWebView的loadFileURL:加载本地文件时无法进入delegate的完成或者失败回调, 原因是使用NSURL的fileURLWithPath:初始化一个本地文件url时的编码问题.

NSURL的fileURLWithPath:自动编码问题

此问题导致在使用WKWebView的loadFileURL:加载本地文件时无法进入delegate的完成或者失败回调, 原因是使用NSURL的fileURLWithPath:初始化一个本地文件url时的编码问题.

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *basePath = [NSString stringWithFormat: @"%@/dist", bundlePath];
NSString *filePath = [NSString stringWithFormat:@"%@/index.html#/searchGoods?q=%@", basePath, @"德国CHERRY樱桃鼠标垫键盘垫LoL游戏FPS竞技电脑笔记本女生办公家用超大加厚加长锁边桌垫小号大号粗面细面"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

这是本地文件filePath的路径:

/Users/bean/Library/Developer/CoreSimulator/Devices/9E7B4ED2-B981-4E96-8AD1-1E5CAC7DD927/data/Containers/Bundle/Application/57E0D718-0DD9-4F47-805D-EA1A0CEAD6FE/economicdaily.app/dist/index.html#/searchGoods?q=%E5%BE%B7%E5%9B%BDCHERRY%E6%A8%B1%E6%A1%83%E9%BC%A0%E6%A0%87%E5%9E%AB%E9%94%AE%E7%9B%98%E5%9E%ABLoL%E6%B8%B8%E6%88%8FFPS%E7%AB%9E%E6%8A%80%E7%94%B5%E8%84%91%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%A5%B3%E7%94%9F%E5%8A%9E%E5%85%AC%E5%AE%B6%E7%94%A8%E8%B6%85%E5%A4%A7%E5%8A%A0%E5%8E%9A%E5%8A%A0%E9%95%BF%E9%94%81%E8%BE%B9%E6%A1%8C%E5%9E%AB%E5%B0%8F%E5%8F%B7%E5%A4%A7%E5%8F%B7%E7%B2%97%E9%9D%A2%E7%BB%86%E9%9D%A2

当使用[NSURL fileURLWithPath:filePath]初始化后自动被编码为:

file:///Users/bean/Library/Developer/CoreSimulator/Devices/9E7B4ED2-B981-4E96-8AD1-1E5CAC7DD927/data/Containers/Bundle/Application/57E0D718-0DD9-4F47-805D-EA1A0CEAD6FE/economicdaily.app/dist/index.html#/searchGoods?q=%E5%BE%B7%E5%9B%BDCHERRY%E6%A8%B1%E6%A1%83%E9%BC%A0%E6%A0%87%E5%9E%AB%E9%94%AE%E7%9B%98%E5%9E%ABLoL%E6%B8%B8%E6%88%8FFPS%E7%AB%9E%E6%8A%80%E7%94%B5%E8%84%91%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%A5%B3%E7%94%9F%E5%8A%9E%E5%85%AC%E5%AE%B6%E7%94%A8%E8%B6%85%E5%A4%A7%E5%8A%A0%E5%8E%9A%E5%8A%A0%E9%95%BF%E9%94%81%E8%BE%B9%E6%A1%8C%E5%9E%AB%E5%B0%8F%E5%8F%B7%E5%A4%A7%E5%8F%B7%E7%B2%97%E9%9D%A2%E7%BB%86%E9%9D%A2

原本filePath中的#和问号被编码,导致无法访问.

解决方法:
使用[NSURL URLWithString:]初始化,但是里面的中文需要单独进行URL encoding

                //获取bundlePath 路径
                NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

                //获取本地html目录 basePath
                NSString *basePath = [NSString stringWithFormat: @"%@/dist", bundlePath];
                // 获取本地html目录 baseUrl
                NSURL *baseUrl = [NSURL fileURLWithPath: basePath isDirectory: YES];

                NSString *filePath = [NSString stringWithFormat: @"%@/index.html#/searchGoods?q=", basePath];

                filePath = [NSString stringWithFormat:@"file://%@", filePath];
                NSString *queryString = [NSString stringWithFormat:@"%@", weakSelf.messageView.model.title];
                queryString = [queryString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                NSURL * URL = [NSURL URLWithString: [NSString stringWithFormat:@"%@%@", filePath, queryString]];
                [vc.wkWebView loadFileURL:URL allowingReadAccessToURL:baseUrl];

此时URL为

file:///Users/bean/Library/Developer/CoreSimulator/Devices/9E7B4ED2-B981-4E96-8AD1-1E5CAC7DD927/data/Containers/Bundle/Application/57E0D718-0DD9-4F47-805D-EA1A0CEAD6FE/economicdaily.app/dist/index.html#/searchGoods?q=%E5%BE%B7%E5%9B%BDCHERRY%E6%A8%B1%E6%A1%83%E9%BC%A0%E6%A0%87%E5%9E%AB%E9%94%AE%E7%9B%98%E5%9E%ABLoL%E6%B8%B8%E6%88%8FFPS%E7%AB%9E%E6%8A%80%E7%94%B5%E8%84%91%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%A5%B3%E7%94%9F%E5%8A%9E%E5%85%AC%E5%AE%B6%E7%94%A8%E8%B6%85%E5%A4%A7%E5%8A%A0%E5%8E%9A%E5%8A%A0%E9%95%BF%E9%94%81%E8%BE%B9%E6%A1%8C%E5%9E%AB%E5%B0%8F%E5%8F%B7%E5%A4%A7%E5%8F%B7%E7%B2%97%E9%9D%A2%E7%BB%86%E9%9D%A2

推荐阅读

目录