您的位置:首页 > 移动开发

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

2012-08-10 17:56 555 查看
有时需要在本地读取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

有问题请留言,大家一起交流学习!
说明:转载请注明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐