当前位置: 代码迷 >> Web前端 >> webView各种展示html、网页
  详细解决方案

webView各种展示html、网页

热度:146   发布时间:2013-10-22 16:17:03.0
webView各种显示html、网页
 一、读取本地html文件----test.html
    //方法一:
     //用loadrequest读取本地.html文件
     NSString *path=[[NSBundle mainBundle]pathForResource:@"test" ofType:@"html"]; 
     NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
     [webView loadRequest:request];   
   
 
    //方法二:
    //用loadhtml读取html代码方式读取
    //取得欲读取档案的位置与文件名
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlpath=[resourcePath stringByAppendingPathComponent:@"test.html"];
    NSString *htmlstring=[NSString stringWithContentsOfFile:htmlpath encoding:NSUTF8StringEncoding error:NULL];//这一段一定要加,不然中文字会乱码
    [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:htmlpath]];
    
    //方法三:
    //文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //使用loadHTMLString()方法显示网页内容
    [webView loadHTMLString:contents baseURL:nil];
    
    
    二、webView展示网页--
    NSString *URL=@"http://www.baidu.com";//样例网址
    NSString *filePathString = [[NSBundle mainBundle]pathForAuxiliaryExecutable:URL];
    NSURL *url = [NSURL URLWithString:filePathString];//
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    [webView loadRequest:request];  

  相关解决方案