IOS开发基础之──读取本地HTML文档

来源:互联网 发布:软件学报 修改后再审 编辑:程序博客网 时间:2024/04/27 14:56

下面将介绍如何如何读取本地端的Html文档


#import  <UIKit/UIKit.h>

 #import  “LoadLocalHtmlViewController.h”


@interfaceLoadLocalHtmlViewController : UIViewController {

    IBOutlet UIWebView*myWebView;

}

@property(nonatomic,retain) UIWebView *myWebView;       

-(IBAction)LoadLocalHtmlFile:(id)sender;

@end

 

 #import  “LoadLocalHtmlViewController.m“

 

@synthesizemyWebView;

-(IBAction)LoadLocalHtmlFile:(id)sender{ 

    //取得欲读取档案的位置与文件名

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

    NSString *filePath =[resourcePath stringByAppendingPathComponent:@"readme.html"];


    //encoding:NSUTF8StringEncoding error:nil 这一段一定要加,不然中文字会乱码

    NSString*htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncodingerror:nil];  

    [myWebViewloadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[ [NSBundle mainBundle] bundlePath]]];

}

-(void)dealloc {

    [myWebView release];

    [super dealloc];

}


readme.html中添加:

<SCRIPTLANGUAGE="JavaScript">

<!--

document.onload= function(){

        document.ontouchmove = function(e){ e.preventDefault(); //-->

</SCRIPT>

 

预设的webview将直接提供使用者进行网贡画面的放大与缩小的操作,若您不希望使用者能进行操作,可加入上述的javascript程序代码来禁止使用,此方法也可用在加入web式的admob或其它广告。