WKWebView 加载沙盒图片和音视频文件失败

先来段html文件(此html直接加载的真机沙盒目录下的一张图,用WKWebView 加载无法显示)

原因:貌似是WebKit现在的bug

解决办法:本地开了一个类似Apache服务器(亲测有效)

步骤如下:

  1. 集成一个工具
 pod 'GCDWebServer', '~> 3.0'
  1. 开启一个本地服务器 (只写了问题相关的代码)
#import // 本地服务器
@interface AppDelegate ()
@property (nonatomic,strong)  GCDWebServer *webSever;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self.webSever startWithPort:80 bonjourName:nil];
}
- (GCDWebServer *)webSever{
    if (!_webSever) {
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                      NSUserDomainMask,
                                                                      YES) objectAtIndex:0];
        _webSever = [[GCDWebServer alloc]init];
        [_webSever addGETHandlerForBasePath:@"/" directoryPath:documentPath indexFilename:nil cacheAge:3600 allowRangeRequests:YES];// 此处设置本地服务器根目录
    }
    return _webSever;
}
@end
  1. 访问沙盒内容时通过服务器路径访问:

方便大家理解,下面是更改前后对比

// 不显示

// 显示

总述:解决的关键就是资源路径~~~。。。。。。

备注:

  1. ' /maya_cache ' 是我建在 Documents 目录下的一个文件夹
  2. http://localhost/ 位置是本地服务器根目录 即之前设置的 Documents
  3. [_webSever addGETHandlerForBasePath:@"/" directoryPath:documentPath indexFilename:nil cacheAge:3600 allowRangeRequests:YES];// 此处设置本地服务器根目录

end 结束~~~(wkwebview坑死啦,特么的~~~~~)

你可能感兴趣的:(WKWebView 加载沙盒图片和音视频文件失败)