您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页UIWebView加载本地html

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];

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务