UIWebview和WKWebview加载本地html

来源:互联网 发布:西安办公软件培训班 编辑:程序博客网 时间:2024/05/21 09:58

情况一 html文件不在文件夹里




  NSURL *url = [[NSBundlemainBundle] URLForResource:@"test"withExtension:@"html"];

        NSURLRequest *request = [NSURLRequestrequestWithURL:url];

        [_webView loadRequest:request];



情况二  html文件在文件夹里



1 添加文件夹时 选择Create folder references



//////// 对于UIWebview的加载方法
@———1
 NSString *documentPath=[[NSBundlemainBundle]bundlePath];
 NSString *filePath = [NSStringstringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm"];
 NSString *htmlString = [NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:nil];
 [self.newsWebViewloadHTMLString:htmlStringbaseURL:[NSURLURLWithString:filePath]];
@———2
 NSString *documentPath=[[NSBundlemainBundle]bundlePath];
 NSString *filePath = [NSStringstringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm”];
 [_webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:filePath]]];

//// 对于WKWebview的加载方法
 NSString *documentPath=[[NSBundlemainBundle]bundlePath];
 NSString *filePath = [NSStringstringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm"];
 NSString *htmlString = [NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:nil];
 [self.newsWebViewloadHTMLString:htmlStringbaseURL:[NSURLURLWithString:@"file:///video-js/"]];
 上一行的   baseURL :::   [NSURL URLWithString:@"file:///文件所在的文件夹名称/"]




如果加载不出来 断点打印路径,,使用前往文件夹的方法看能不能成功进入,,调整路径拼接直到可以前往便能成功加载






 










原创粉丝点击