当前位置: 代码迷 >> Web前端 >> iphone开发之运用UIWebView显示html内容
  详细解决方案

iphone开发之运用UIWebView显示html内容

热度:319   发布时间:2012-09-02 21:00:34.0
iphone开发之使用UIWebView显示html内容
有时需要在本地读取html文件或者从服务器端获取帮助信息这一类的页面显示在视图中,我们可以使用UIWebView 中的

loadHTMLString方法来实现。

代码如下:

//
//  ViewController.m
//  UIWebViewTest
//
//  Created by Cloay on 12-8-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 100, 320, 360)] autorelease];
    
    //使用loadHTMLString()方法显示网页内容
    [webView loadHTMLString:[self getHtmlString] baseURL:nil];

    [self.view addSubview:webView];
}
    //读取html文件内容
- (NSString *)getHtmlString{
    //文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    
    NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    return contents;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

有问题请留言,大家一起交流学习!
说明:转载请注明出处!

  相关解决方案