IOS..读取本地HTML文档

加载本地HTML文件并应用JavaScript事件处理
本文介绍如何在iOS应用中使用Swift语言通过UIKit框架加载本地HTML文件,并实现JavaScript事件处理,确保中文字符正确显示且防止触摸事件误触发。
#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>

 

转载于:https://www.cnblogs.com/iflewless/p/3925183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值