IOS webview控件使用简介(二)–加载本地html


下文主要讲述UIView加载本地html文件的方法

    //1 -----从文件中加载 html
    //生成一个webView
    UIWebView *webViewTest = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)];
    
    webViewTest.opaque = YES;
    webViewTest.delegate = self;
    
    NSString *strTest = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]; //获取加载test.html
    
    [webViewTest loadHTMLString:[NSString stringWithContentsOfFile:strTest encoding:NSUTF8StringEncoding error:nil] baseURL:[NSURL fileURLWithPath:strTest]];
     //加载本地html    

    [self.view addSubview:webViewTest];
    webViewTest = nil;
    strTest =nil; 
    NSLog(@"webView控件生成并加载本地html!");
  
     //2 ------从字符中加载html 
         //生成一个webView
    UIWebView *webViewTest = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)];
    
    webViewTest.opaque = YES;
    webViewTest.delegate = self;
    
    NSURL *urlTest = [NSURL fileURLWithPath:@"/html/image"]; //设置资源文件路径
    [webViewTest loadHTMLString:@"加载html成功" baseURL:urlTest];
    
    [self.view addSubview:webViewTest];
    webViewTest = nil;
    
    NSLog(@"webView控件生成并加载本地html!");