您的位置:首页 > 移动开发

UIWebview和WKWebview加载本地html

2017-12-20 15:49 513 查看

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



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

NSURLRequest *request = [NSURLRequest
requestWithURL:url];

[_webView loadRequest:request];

情况二 html文件在文件夹里



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

//////// 对于UIWebview的加载方法
@———1
NSString
*documentPath=[[NSBundle
mainBundle]
bundlePath];

NSString
*filePath = [NSString
stringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm"];

NSString
*htmlString = [NSString
stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];

[self.newsWebView
loadHTMLString:htmlString
baseURL:[NSURL
URLWithString:filePath]];

@———2

NSString
*documentPath=[[NSBundle
mainBundle]
bundlePath];

NSString
*filePath = [NSString
stringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm”];

[_webView
loadRequest:[NSURLRequest
requestWithURL:[NSURL
URLWithString:filePath]]];

//// 对于WKWebview的加载方法
NSString
*documentPath=[[NSBundle
mainBundle]
bundlePath];

NSString
*filePath = [NSString
stringWithFormat:@"%@%@",documentPath,@"/video-js/index.htm"];

NSString
*htmlString = [NSString
stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];

[self.newsWebView
loadHTMLString:htmlString
baseURL:[NSURL
URLWithString:@"file:///video-js/"]];
上一行的 baseURL ::: [NSURL URLWithString:@"file:///文件所在的文件夹名称/"]













如果加载不出来 断点打印路径,,使用前往文件夹的方法看能不能成功进入,,调整路径拼接直到可以前往便能成功加载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: