ios webview加载html 怎么加载html

IOS之UIWebView的使用
刚接触开发1年多,现在对于混合式移动端开发越来越流行,因为开发成本上、速度上都比传统的APP开发要好,混合式开发是传统模式与PC网页端相结合的模式。那么提到了 APP的混合模式开发,在开发中有WebView作为混合模式开发的桥梁,当然在IOS中也同样有一个 UIWebView 来作为混合模式开发的桥梁,那么下面就对UIWebView的一些基本知识详解一下。
一、UIWebView的基础使用
1、创建UIWebView:
CGRect bouds = [[UIScreen manScreen]applicationFrame];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
2、设置属性:
webView.scalespageToFit = YES;//自动对页面进行缩放以适应屏幕
webView.detectsPhoneNumbers = YES;//自动检测网页上的电话号码,单击可以拨打
3、显示网页视图UIWebView:
[self.view addSubview:webView];
4、加载内容
NSURL* url = [NSURL URLWithString:@&&];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载
也可以加载一个本地资源:
NSURL* url = [NSURL fileURLWithPath:filePath];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载
UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@&&]];
UIWebView类内部会管理的导航动作,通过goForward和goBack方法你可以控制前进与后退动作:
[webView goBack];
[webView goForward];
[webView reload];//重载
[webView stopLoading];//取消载入内容
6、UIWebViewDelegate委托代理
UIWebView支持一组委托方法,这些方法将在特定时间得到通知。要使用这些方法,必须先设定webView的委托:
webView.delegate =
下面每个委托方法的第一个参数都是指向一个UIwebview的指针,因此你可以将一个委托用于多个网页视图。
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationT//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值:
UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther
UIWebView控件加载网页的监听函数方法:
-(void)webViewDidStartLoad:(UIWebView*)webV//当网页视图已经开始加载一个请求后,得到通知。
-(void)webViewDidFinishLoad:(UIWebView*)webV//当网页视图结束加载一个请求之后,得到通知。
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)//当在请求加载中发生错误时,得到通知。会提供一个NSSError对象,以标识所发生错误类型。
以上是IOS中UIWebView的基础使用要点详解,接下来一些UIWebView的常用注意点。
二、IOS中UIWebView常用注意点:
1、与UIWebView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号:
-(void)webViewDidFinishLoad:(UIWebView *)webView
NSString *sendJsStr=[NSString stringWithFormat:@&openFile(\&%@\&)&,jsDocPathStr];
[webView stringByEvaluatingScriptFromString:sendJsStr];
2、在该代理方法中判断与webView的交互,可通过html里定义的协议实现:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
3、只有在webView加载完毕之后在能够调用对应页面中的js方法。(对应方法如第1条).
4、为webView添加背景图片:
approvalWebView.backgroundColor=[UIColor clearColor];
approvalWebView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明 在webView下添加个imageView展示图片就可以了
5、获取webView页面内容信息:
NSString *docStr=[webView stringByEvaluatingFromString:@&document.documentElement.textContent&];//获取web页面内容信息,此处获取的是个json字符串
SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
NSDictionary *contentDic=[parserJson objectWithString:docStr];//将json字符串转化为字典
6、 加载本地文件的方法:
//第一种方法:
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@&html& inDirectory:@&mobile&];//mobile是根目录,name是文件名称,html是文件类型
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //加载本地文件
//第二种方法:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@&mobile.html&];
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
7、将文件下载到本地址然后再用webView打开:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@&Documents&]];
self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@&maydoc%@&,docType]];
NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];
[attachmentData writeToFile:filePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[attachmentWebView loadRequest:requestObj];
//删除指定目录下的文件
NSFileManager *magngerDoc=[NSFileManager defaultManager];
[magngerDoc removeItemAtPath:filePath error:nil];
8、处理webView展示txt文档乱码问题:
if ([theType isEqualToString:@&.txt&])
//txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt
//不带的,可以依次尝试GBK和GB18030编码
NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
if (!aStr)
//用GBK进行编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x];
if (!aStr)
//用GBK编码不行,再用GB18030编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x];
//通过html语言进行排版
NSString* responseStr = [NSString stringWithFormat:
[attachmentWebView loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
9、使用webView加载本地或网络文件整个流程:
1、 Loading a local PDF file into the web view
- (void)viewDidLoad {
[super viewDidLoad];
//从本地加载
NSString *thePath = [[NSBundle mainBundle] pathForResource:@&iPhone_User_Guide& ofType:@&pdf&];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:@&application/pdf&
textEncodingName:@&utf-8& baseURL:nil];
//从网络加载
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@&/&]]];
2、The web-view delegate managing network loading
- (void)webViewDidStartLoad:(UIWebView *)webView
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- (void)webViewDidFinishLoad:(UIWebView *)webView
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
@&An error occurred:
error.localizedDescription];
[self.myWebView loadHTMLString:errorString baseURL:nil];
3、Stopping a load request when the web view is to disappear
- (void)viewWillDisappear:(BOOL)animated
if ( [self.myWebView loading] ) {
[self.myWebView stopLoading];
self.myWebView.delegate = // disconnect the delegate as the webview is hidden
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
/************/
引用自苹果官方文档(displaying web content)
10、查找webView中的scrollview:
- (void) addScrollViewListener
UIScrollView* currentScrollV
for (UIView* subView in self.webView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
currentScrollView = (UIScrollView*)subV
currentScrollView.delegate =
11、去掉webView的阴影,做成类似scrollView:
- (void)clearBackgroundWithColor:(UIColor*)color
// 去掉webview的阴影
self.backgroundColor =
for (UIView* subView in [self subviews])
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
12、取消长按webView上的链接弹出actionSheet的问题:
-(void)webViewDidFinishLoad:(UIWebView *)webView
[webView stringByEvaluatingJavaScriptFromString:@&document.documentElement.style.webkitTouchCallout = 'none';&];
13、取消webView上的超级链接加载问题:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if (navigationType==UIWebViewNavigationTypeLinkClicked) {
return NO;
return YES;
14、webView在ios5.1之前的bug:在之前的工程中使用webView加载附件,webView支持doc,excel,ppt,pdf等格式,但这些附件必须先下载到本地然后在加载到webView上才可以显示, 当附件下载到本地之后刚刚开始加载到webView上时,此时退出附件页面会导致程序崩溃。会崩溃是由于webView控件内部没有把相关代理取消掉,所以导致退出之后程序崩溃。
webView在5.1上的bug:之前项目需求要webView可以左右活动,但在往webView上加载页面时导致页面加载不全,这个bug是由于webView本身的缓存所致。(还有待研究)
15、在使用webView进行新浪微博分享时,webView会自动保存登陆的cookie导致项目中的分享模块有些问题,删除 webView的cookie的方法:
-(void)deleteCookieForDominPathStr:(NSString *)thePath
//删除本地cookie,thePath为cookie路径通过打印cookie可知道其路径
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:thePath]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
16、在UIWebView中使用flashScrollIndicators
使用UIScrollView时,我们可以使用flashScrollIndicators方法显示滚动标识然后消失,告知用户此页面可以滚动,后面还有 更多内容。UIWebView内部依赖于UIScrollView,但是其没有flashScrollIndicators方法,但可以通过其他途径使用 此方法,如下所示。
for (id subView in [webView subviews])
{ if ([subView respondsToSelector:@selector(flashScrollIndicators)])
[subView flashScrollIndicators];
上述代码片段可以到webViewDidFinishLoad回调中使用,加载完网页内容后flash显示滚动标识。
17、根据内容获取UIWebView的高度:
有时候需要根据不同的内容调整UIWebView的高度,以使UIWebView刚好装下所有内容,不用拖动,后面也不会留白。有两种方式可根据加载内容 获取UIWebView的合适高度,但都需要在网页内容加载完成后才可以,即需要在webViewDidFinishLoad回调中使用。
①.使用sizeThatFits方法。
- (void)webViewDidFinishLoad:(UIWebView *)webView
CGRect frame = webView.
frame.size.height = 1;
webView.frame =
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingS
webView.frame =
sizeThatFits方法有个问题,如果当前UIView的大小比刚好合适的大小还大,则返回当前的大小,不会返回最合适的大小值,所以使用 sizeThatFits前,先将UIWebView的高度设为最小,即1,然后再使用sizeThatFits就会返回刚好合适的大小。
②、使用JavaScript
- (void)webViewDidFinishLoad:(UIWebView *)webView
{ CGRect frame = webView.
NSString *fitHeight = [webview stringByEvaluatingJavaScriptFromString:@&document.body.scrollH&];
frame.size.height = [fitHeight floatValue];
webView.frame =
首先 对IOS开发中的UIWebView控件的基本使用进行初步的详解,提到了创建、设置属性、设置背景、怎么样
加载网页内容等一系列的基础点,然后阐述使用UIWebView控件时常用用注意点,经常需要用到的地方,需要注意
的地方,使得对开发ios APP混合模式的桥梁---UIWebView控件更加的了解、熟悉。UIWebView既能够加载服务器
提供的URI,又能够加载本地的资源文件,还能够加载服务器返回的网页界面代码,可想而知UIWebView是多么强
大的一控件桥梁,以后在开发中使用到的地方会越来越多。&&&&&&&&&&&
iOS之在webView中引入本地html,image,js,css文件的方法
最近开发的项目,需要一个webView,同时这个webView会需要引入一些项目中的资源:
一个本地的html文件,作为webView的模板
两张loading图片,在图片未加载的时候进行占位
jquery.js,scrollLoading.js 也是本地的,实现滚动加载图片功能
然后就开始了漫长的Google历程。
在webView中引入本地的html文件
这里最主要的一个webView的方法是:loadHTMLString:baseURL:&把HTML文件的内容以字符串的形式加载到webView里面,然后解析。
// get the model which is a html file for the webViewNSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];// load the html file to webView[_webView loadHTMLString:htmlCont baseURL:nil];
通过上述方法,很方便的就能把一个HTML文件加载到webView中,很简单吧,接下来,来点进阶功能!
在webView中引入本地的image文件
这个功能的实现,很大程度上是借鉴了这篇文章:UIWebView & Loading External Images and CSS。大家可以去看看这篇文章,或者看我下面的继续描述。
这个功能的实现是承接上面那个方法的进一步扩展,最关键的是那个baseURL。先看实现代码:
// get the model which is a html file for the webViewNSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
// 获取当前应用的根目录NSString *path = [[NSBundle mainBundle] bundlePath];NSURL *baseURL = [NSURL fileURLWithPath:path];
// 通过baseURL的方式加载的HTML// 可以在HTML内通过相对目录的方式加载js,css,img等文件[_webView loadHTMLString:htmlCont baseURL:baseURL];
在object-c里面通过如上面的方式加载HTML文件,指定了baseURL的值为程序的bundlePath,然后在HTML文件里面就可以自由的通过直接书写标签的方式加载图片图片文件了
&img src="loading.png" /&;
要注意的是:所有在应用内的资源文件都是在baseURL的根目录也就是此代码中的bundlePath的根目录,所以图片资源,不管在项目里面放在哪个目录结构下,在HTML内引用的时候,都是直接根目录的。
baseURL到底是什么东西?对此,我也很好奇,所以我NSLog了代码里面的baseURL,然后得到的结果是:file:///Users/(用户名)/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/(一些大写字母加连字符加数字的序列号)/(应用名).app/ 。然后我在终端里面找到这个目录,打开一看,发现都是一些HTML,image,txt等静态资源。
bundlePath和其中的内容展示
至此,在webView中插入本地image资源的功能已经实现了,下面是更有挑战性的功能:添加js文件
添加本地js文件到webView中
这个实现说起来其实很简单,因为不需要任何代码层面上的修改,只需要按上面添加image的方式,在script的src里面直接写js的文件名即可。
但是如果直接这样写,你就会发现js资源根本没有被加载。到底image和js有什么区别?看上面的图片,可以看到默认在bundlePath里面是没有我引入到工程里面的jquery.js和scrollLoading.js的。那么,这个是不是导致js资源没有被正确加载的原因?
在这篇文章:How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class和这篇文章iPhone基于lightbox的图片放大特效和网页布局中,都提及到一个
Select .js file and in the &Detail& view unselect the bullseye column indicating it is compiled code
In the &Groups & files& view expand the &Targets& tree and expand the application then go to &Copy Bundle Resources& and drag the *.js files into it.
方法是有了,可是这种英文的描述,还没有附加图片,实在是让人看不懂,大致知道的就是:js文件在xcode里面,默认是一种需要被编译的文件,这就导致它不会被放到我们刚刚放到的BundlePath(更专业的名称应该是Bundle Resources)里。
所以要解决的问题是,怎样才能使得js文件不被编译并且放到Bundle Resources中。
期间为了理解上面那两句英文而Google的经历就不说了,直接说结果吧。
在xcode里面,每个project都有至少一个Targets(多个的也有,但是我不懂),在Targets里面(打开Targets的方式是在左侧栏,点击project,在中间的内容区,就会出现project喝Targets),存放了一些资源文件,在Build Phases下可以看到,跟本次内容关联最大的有两项:Compile Sources和Copy Bundle Resources。在没修改的情况下,展开Compile Sources就能看到找了很久的jquery.js和scrollLoading.js
打开Targets(基于xcode5的界面)展开Compile Sources后能看到两个js文件
接下来要做的很简单,从Compile Sources中删除两个js文件,再在Copy Bundle Resources中添加这两个文件,一切搞定。想来(偷懒,不想Google继续深入了解了),Compile Sources是放置那些需要被编译的文件,.h,.m和冤枉的.js文件等等,而Copy Bundle Resources里面放的是一些资源文件,在程序在运行时会引入的,同时在项目打包之后也依旧存在的文件。
其他格式的资源文件,在添加的时候也大致就是这个流程,不重复说了。
阅读(...) 评论()主题 : webView加载html代码,怎么让图片和文字显示正常
级别: 侠客
UID: 491067
可可豆: 685 CB
威望: 457 点
在线时间: 489(时)
发自: Web Page
来源于&&分类
webView加载html代码,怎么让图片和文字显示正常&&&
如果没有做自适应的话,如果图片的尺寸按照他原来的大小加载,做了自适应,图片显示正常,但是字体太小。
描述:这是做了自适应的字体
图片:98B43B52-5E15-C1B5CEA3BE2.png
描述:没有做自适应的字体
图片:F2C31F8E--EC404FED.png
级别: 侠客
UID: 491067
可可豆: 685 CB
威望: 457 点
在线时间: 489(时)
发自: Web Page
我自己来回答,,,如果webView要加载图片,但是图片过大,就对webView做自适应,这样图片就显示正常,问题又来了,字体显示的特别小,这样就要在- (void)webViewDidFinishLoad:(UIWebView *)webView {&&&&NSString *str = @&document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'&;&&&&[_webView stringByEvaluatingJavaScriptFromString:str];} 代理方法里面设置。这样做了之后就会发现,图片显示正常了,字体也显示正常了,webView的高度也显示正常了。。
级别: 侠客
UID: 493847
可可豆: 652 CB
威望: 637 点
在线时间: 578(时)
发自: Web Page
回 1楼(为了取悦自己) 的帖子
这个方法可以 但是doc文件的加载 显示就有点 太大了
级别: 侠客
UID: 490460
可可豆: 375 CB
威望: 265 点
在线时间: 494(时)
发自: Web Page
如果是加载富文本的话可以在富文本前加这句:&head&&style&img{max-width:100% !} table{max-width:100% !}&/style&&/head&
级别: 侠客
UID: 432535
可可豆: 540 CB
威望: 518 点
在线时间: 132(时)
发自: Web Page
回 楼主(为了取悦自己) 的帖子
楼主是怎么用webview加载图文共存的字符串格式的html的
级别: 圣骑士
UID: 532081
可可豆: 1441 CB
威望: 1098 点
在线时间: 470(时)
发自: Web Page
回 4楼(luo999) 的帖子
你这个问题解决了吗?
级别: 侠客
可可豆: 229 CB
威望: 219 点
在线时间: 792(时)
发自: Web Page
还是让做wap页面的调下吧.不然就自己写页面.最烦这种wap页面了.
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版UIWebView加载html -
- ITeye博客
博客分类:
UIWebView加载html并调用js,html中需动态行进传值,用字符串格式方式动态添加,下面代码中有相关实现.
stringByEvaluatingJavaScriptFromString方法作用是返回运行后js结果,使用请见代码。
ViewController.h
WebViewTest
UIWebView加载html并调用js
Created by
on 12-10-30.
Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UIWebViewDelegate&
@property (weak, nonatomic) IBOutlet UIWebView *webV
@property (strong,nonatomic) NSMutableArray *arrayL
#pragma 方法
//创建webView内容方法
- (void)createWebViewC
//创建语言界面页
- (NSString*)createLanguageP
//返回运行后js结果
- (void)executeJSFunction:(NSString *)
ViewController.m
WebViewTest
Created by
on 12-10-30.
Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@implementation ViewController
@synthesize webV
@synthesize arrayL
- (void)viewDidLoad
[super viewDidLoad];
arrayLanguages = [[NSMutableArray alloc] initWithCapacity:25];
[self createWebViewContent];
//创建web内容
- (void)createWebViewContent{
NSString *homePath = [[NSBundle mainBundle] executablePath];
NSArray *strings = [homePath componentsSeparatedByString: @"/"];
NSString *executableName
= [strings objectAtIndex:[strings count]-1];
NSString *rawDirectory = [homePath substringToIndex:
[homePath length]-[executableName length]-1];
NSString *baseDirectory = [rawDirectory stringByReplacingOccurrencesOfString:@" "
withString:@"%20"];
NSString *imagePath = [NSString stringWithFormat:@"file://%@/map.png",baseDirectory];
NSLog(@"imagePath: %@",imagePath);
NSString *htmlFile = [NSString stringWithFormat:@"file://%@/welcome.html",baseDirectory];
NSLog(@"htmlFile: %@",htmlFile);
NSURL *url = [NSURL URLWithString: htmlFile];
NSData *fileData = [NSData dataWithContentsOfURL:url];
//NSData *fileData = [NSData dataWithContentsOfFile:htmlFile];
NSLog(@"fileData: %@",fileData);
NSString *htmlContent = [[NSMutableString alloc] initWithData:
fileData encoding:NSUTF8StringEncoding];
NSLog(@"htmlContent: %@",htmlContent);
NSString *langContent = [self createLanguagePage];
//通过字符串方式还对html进行动态传值
NSString* string =
[NSString stringWithFormat: htmlContent,imagePath,langContent];
NSLog(@"string:%@",string);
//Load the HTML String on UIWebView
[self.webView loadHTMLString:string baseURL:nil];//加载html字符串到UIWebView上(该方法极为重要)
//Adjust position
CGRect bounds = [[UIScreen mainScreen] bounds];
int screenWidth =
bounds.size.
int screenHeight =
bounds.size.
self.webView.frame = CGRectMake(0, 20,screenWidth, screenHeight);
//创建语言显示页
- (NSString*)createLanguagePage{
arrayLanguages = [[NSMutableArray alloc] initWithObjects:
@"English",@"Deutsch",@"Fran?ais",@"Espa?ol",@"Italiano",@"Pусский",@"日本語",@"中文",
@"???????",@"?????",@"ελληνικ?",@"??????",@"???",@"Bahasa Indonesia",@"Nederlands",@"Norsk",
@"????",@"Polski",@"Português",@"Svenska",@"?????",@"?esky",@"Türk?e",@"Tiê?ng Viê?t",@"?????&",nil];
NSMutableString* string =[[NSMutableString alloc]initWithCapacity:1024];
//init a mutable string, initial capacity is not a problem, it is flexible
for(int i = 0; i & [arrayLanguages count]; i++){
[string appendString:@"&a href=\"javascript:void(0)\" "];
[string appendString:@"style=\"color:text-decoration: "];
[string appendString:@"font-family:'Helvetica'; font-size:14\" "];
[string appendString:[NSString stringWithFormat:
@" onMouseDown=\"imageClicked('L%d')\"&%@&/a&",
i,[arrayLanguages objectAtIndex:i]]];
[string appendString:@"&&|&&"];
if(i%3 == 0)
[string appendString:@"&br&"];//换行
//返回运行后js结果
- (void)executeJSFunction:(NSString *)jsCommand{
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
NSLog(@"result:%@",result);
//webview开始加载请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//linking the javascript to call the iPhone control
NSString *url = request.mainDocumentURL.relativeP
if(url != nil) {
NSLog(@"url:%@",url);
NSArray *strings = [url componentsSeparatedByString: @"/"];
NSString *token
= [strings objectAtIndex:[strings count]-1];
NSString *text = @"";
if([token hasPrefix:@"L"]){
int i = [[token substringFromIndex:1] intValue];
text = [NSString stringWithFormat:@"language:%@ be selected",
[arrayLanguages objectAtIndex:i]];
text = [NSString stringWithFormat:@"Area:%@ be selected",token];
//执行js后,得到html里title内容(通过执行js来获取html中的内容)
NSString *title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"title :%@",title);
NSString *imgSrc = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('topimage')."];
NSLog(@"imgSrc :%@",imgSrc);
UIAlertView* alert=
[[UIAlertView alloc]initWithTitle:@"javascript message"
message:text
delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
return FALSE;
return TRUE;
welcome.html
&meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no, width = 320"/&
&title&How to build an iPhone website&/title&
function imageClicked(i){
var clicked =
var str = ""+i;
if(str.charAt(0) != 'L'){
var id = 'area' +
var el = document.getElementById(id);
alert("调用js : "+i);
window.location="http://click/"+i;
function printStr(str){
&style type="text/css"&
.borderImage {
-webkit-tap-highlight-color:rgba(0,0,0,0);
&body style="background-color:margin-top:0margin-left:0px"&
&div class="borderImage"&
&img id="topimage" src="%@
" border="0" height="241" width="319" usemap="#map"&
&map name=map&
&area shape="rect" id="area0" coords="124,1,191,58" href="javascript:imageClicked(0)" /&
&area shape="rect" id="area1" coords="36,64,97,110" href="javascript:imageClicked(1)" /&
&area shape="rect" id="area2" coords="119,135,171,182" href="javascript:imageClicked(2)" /&
&area shape="rect" id="area3" coords="205,26,248,60" href="javascript:imageClicked(3)" /&
&area shape="rect" id="area4" coords="264,88,306,132" href="javascript:imageClicked(4)" /&
&area shape="rect" id="area5" coords="272,154,313,199" href="javascript:imageClicked(5)" /&
&area shape="rect" id="area6" coords="187,125,232,172" href="javascript:imageClicked(6)" /&
&area shape="rect" id="area7" coords="255,1,285,26" href="javascript:imageClicked(7)" /&
&area shape="rect" id="area8" coords="283,1,314,26" href="javascript:imageClicked(8)" /&
完整工程请见附件!
(124.4 KB)
下载次数: 134
浏览 14107
wenxin2009
浏览: 187879 次
来自: 上海
你这东西直接拷贝过来,还一大堆注释的代码,而且没有解释说明,怎 ...
PerfectHand 写道为什么没有 sql那一部分呢 求s ...
sql那部分就跟平常sql一样,一个update语句
为什么没有 sql那一部分呢 求sql 那一部分
有源代码吗?麻烦上传一下,谢谢了!

我要回帖

更多关于 wkwebview加载html 的文章

 

随机推荐