UIWebView加载本地html

UIWebView除了能加载网页地址外还可以加载本地html,加载的方式主要有两种

读取本地html内容,加载内容

NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSString *htmlPath = [NSString stringWithFormat:@"%@/index.html", mainBundlePath];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString: htmlContent baseURL:baseURL];

通过本地html地址加载内容

NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[_webView loadRequest:request];

有些情况下,我们不仅要能加载本地html,还需要给本地html传递参数,这种情况下我们只能通过方法二来进行参数传递

NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
NSString *queryString = [NSString stringWithFormat:@"%@/index.html?key=value", mainBundlePath];
NSURL *queryURL = [NSURL URLWithString:queryString];
NSURLRequest *request = [NSURLRequest requestWithURL:queryURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[_webView loadRequest:request];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容