zoukankan      html  css  js  c++  java
  • 使用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]];



  • 相关阅读:
    Linux面试题汇总答案
    VMWARE ESXI 虚拟硬盘的格式:精简置备、厚置备延迟置零、厚置备置零
    [Python基础知识]正则
    [代码评审1]代码评审
    [EF2]Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
    [EF1]POCOs(Plain Old C# Object)Entity Framework 4.x: POCOs入门
    [网站性能3]SqlServer中Profiler的使用
    [网站性能2]Asp.net平台下网站性能调优的实战方案
    [网站性能1]对.net系统架构改造的一点经验和教训
    2.1 python使用MongoDB 示例代码
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7203103.html
Copyright © 2011-2022 走看看