关于 HTML 离线模式简要设置

直接上干货

允许网页加载本地资源

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    _mWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height) configuration:config];

加载沙盒html文件并传参

-(void)getFromLocalFile:(NSString *)custom_id{
    
    NSString *documnet = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/"];
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pano"];
    NSString *tcPath = [path stringByAppendingPathComponent:@"/detail.html"];
    
    if ([NSFileManager.defaultManager fileExistsAtPath:tcPath]) {
        ///加载沙盒html并传参
        NSURL * url = [NSURL fileURLWithPath:tcPath isDirectory:NO];
        NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
        //单一参数
        [urlComponents setQuery:[NSString stringWithFormat:@"hall_id=%@",custom_id]];
        //多参
        //    NSMutableArray *queryItemArray = [NSMutableArray array];
        //    [queryItemArray addObject:[NSURLQueryItem queryItemWithName:@"hall_id" value:@"5801"]];
        //    [urlComponents setQueryItems:queryItemArray];
        [_mWebView loadFileURL:urlComponents.URL allowingReadAccessToURL:[NSURL fileURLWithPath: documnet]];
    } else {
        NSLog(@"没有文件");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容