`

UIWebView和网页交互的研究初探

 
阅读更多

很多时候,我们使用UIWebView不是简单的显示一个网页这么简单的,我们需要更多的交互工作,譬如能否通过Objective-C的代码来控制网页的显示内容,能否监控网页上的点击事件等等,这里我是根据网上的资料做的演示,因此会有很多和网络已有的资料相同的地方。

此外,关于UIWebView的基本使用方面,请看我的另外一篇文章

iOS5编程--官方例子代码的研究--2.UICatalog-6

首先我们需要准备一个html文件,文件名称为1.html,内容如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>test UIWebView and JS+html</title>

<style type="text/css">

</style>

</head>

<body>

<h1>测试</h1>

<center><a href="/1">调用Objective-C</a></center>

<center><a href="http://www.hotmail.com">hotmail</a></center>

<form name="myform">

<input id="field_1" type="text" name="input1" value = "测试"/><br/><br/><br/>

<input id="field_2" type="text" name="input2" /><br/><br/><br/>

<input id="field_3" type="text" name="input3" /><br/><br/><br/>

</form>

</body>

</html>

其次根据上面的html内容,我们必须知道相关的javascript的知识,比如通过document.title可以得到上面网页的title。

建立一个View-Based Application,命名为tWeb,把1.html加入到工程中去,然后在tWebViewController上放上一个web view和一个button,如下所示


做好相关的链接和事件的响应,所以tWebViewController.h文件如下:

@interface tWebViewViewController : UIViewController<UIWebViewDelegate> {

UIWebView *myWebView;

}


@property (nonatomic, retain) IBOutlet UIWebView *myWebView;

- (IBAction)be:(id)sender;


@end



我先贴出来tWebViewController.m的内容再解释。

@implementation tWebViewViewController

@synthesize myWebView;


- (void)didReceiveMemoryWarning

{

// Releases the view if it doesn't have a superview.

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle

- (void)viewDidLoad {

[super viewDidLoad];

self.myWebView.delegate=self;

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"html"];

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return YES;

}


- (void)viewDidUnload {

self.myWebView=nil;

}


- (void)dealloc {

[self.myWebView release];

[super dealloc];

}

#pragma mark –

#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

if ( [request.mainDocumentURL.relativePath isEqualToString:@"/1"] ) {

UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"成功"

message:@"从网页中调用的" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];

[alert show];

[alert release];

return false;

}

return true;

}

- (void)webViewDidStartLoad:(UIWebView *)webView

{

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

NSLog(@"title=%@",title);

//NSString *st = [ webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('field_1').value"];

NSString *st = [webView stringByEvaluatingJavaScriptFromString:@"document.myform.input1.value"];

NSLog(@"st =%@",st);

//添加数据

[myWebView stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('field_2');"

"field.value='OC码写入';"];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

}

- (IBAction)be:(id)sender {

[myWebView stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('field_3');"

"field.value='test';"];

}



再viewDidLoad的时候,我们调用函数来是的webview load 1.html,因为我们设置web view的delegate是tWebViewController,因此,再下面的函数就是这个delegate定义的函数。

函数- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

是在webview发起任何一个请求的时候被调用,哪怕是第一次load 1.html的时候也会被调用。通过request.mainDocumentURL.relativePath可以得到本次请求的url,因此在1.html中由如下代码。

<center><ahref="/1">调用Objective-C</a></center>

<center><ahref="http://www.hotmail.com">hotmail</a></center>


这里由两个链接,一个是我们自定义的格式,一个是普通的http的url。你通过代码就可以看出,如果我们需要hold自定义的格式,就让这个函数返回false,这样web view就不会有进一步的动作。

函数

- (void)webViewDidStartLoad:(UIWebView*)webView

是在将要load一个url的时候被调用。

函数

- (void)webViewDidFinishLoad:(UIWebView*)webView

是在一个url被完全load近来后被调用。

所以这里的代码更多。看第一行代码NSString*title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];,其中函数stringByEvaluatingJavaScriptFromString可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互,需要等UIWebView中的页面加载完成之后去调用。这里第一行代码就是得到了1.html的title.,下面的都是依此类推了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics