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




    大家都知道,使用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]];



  • 相关阅读:
    网页抓取
    基本数据结构
    小节
    顺序统计量
    线性时间排序
    快速排序
    堆排序 Heapsort
    大数运算
    趣味题,文本中洞的数量
    nginx config配置
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7203103.html
Copyright © 2020-2023  润新知