码迷,mamicode.com
首页 > Web开发 > 详细

使用UIWebView载入本地或远程server上的网页

时间:2017-07-18 23:05:19      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ror   ase   bundle   base   网页   data-   cep   err   content   



大家都知道,使用UIWebView载入本地或远程server上的网页,sdk提供了三个载入接口:

- (void)loadRequest:(NSURLRequest *)request; 
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;


- (void)loadRequest:(NSURLRequest *)request; 
这个接口一般用于载入url所指定的某个远程server网页,事实上它也能用来载入本地网页资源。

//载入远程网页
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];

//载入本地网页
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

注意:网页可能会使用其它的如图片资源,css样式文件,loadRequest会在网页的当前文件夹下(如本例中的XiangJie文件夹下查找)


- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
这个接口用于直接载入html代码。假设html直接写在了代码中,推荐使用这样的方法。

当然你也能够先从本地读取html代码,然后载入。

请注意baseURL地址文件夹要正确,否则html中引用的资源是找不到的。 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"]; NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XiangJie/img"]; [_myWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:baseURL]]; - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL; 这个接口用于载入html文件。

MIMEType值一般为"text/html"。相同。请注意baseURL地址文件夹要正确。否则html中引用的资源是找不到的。 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kline71" ofType:@"html" inDirectory:@"XiangJie"]; NSData *data = [NSData dataWithContentsOfFile:filePath]; NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XiangJie/img"]; [self.myWebView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:baseURL]];



使用UIWebView载入本地或远程server上的网页

标签:ror   ase   bundle   base   网页   data-   cep   err   content   

原文地址:http://www.cnblogs.com/lxjshuju/p/7203103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!