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];  

你可能感兴趣的:(html,webView,html文件,网页显示)