UIWebview和WKWebview加载本地html

本文介绍如何在iOS应用中使用UIWebView和WKWebView加载本地HTML文件。通过NSBundle获取HTML文件URL,然后创建NSURLRequest并加载到WebView中进行显示。

情况一 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:///文件所在的文件夹名称/"]




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






 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值