>  기사  >  웹 프론트엔드  >  UIWebView加载Html_html/css_WEB-ITnose

UIWebView加载Html_html/css_WEB-ITnose

WBOY
WBOY원래의
2016-06-21 08:47:071165검색

苹果自带的UIWebView提供了基本的Html访问功能,提供了三种方法:

- (void)loadRequest:(NSURLRequest *)request;- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
##加载本地Html 方式1:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; NSString *filePath =[resourcePath stringByAppendingPathComponent:@"FlyElephant.html"]; NSMutableString *htmlstring=[[NSMutableString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSURL *baseUrl=[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; [self.webView loadHTMLString:htmlstring baseURL:baseUrl];
方式2:
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Book" ofType:@"html"]; NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
###加载网络Html 通过loadRequest实现:
NSURL *url = [[NSURL alloc] initWithString:@"https://www.baidu.com/"]; [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
#加载NSData loadData使用起来也非常简单:
NSString *path = [[NSBundle mainBundle]pathForResource:@"Book.pdf" ofType:nil]; //以二进制的形式加载数据 NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];

友情提示,如果是新建项目,注意在info.list中添加NSAppTransportSecurity为Dictionary,NSAllowsArbitraryLoads为Boolean:

FlyElephant.png

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.