javascript
ios UIWebView调用本地html和javascript,并且和ios通讯
ios和android都提供了有關webview和javascript通訊的功能,這就使開發者根據手機的系統展示適合手機的界面,是界面開發更加簡單。
我的原型主要實現通過UIWebView展示本地的html、css、javascript文件,并且和ios互相通訊,用來展示數據。
下面是我實現的一個簡單demo,界面效果如下:
點擊連接調用ios中的提醒功能:
實現過程:
- 首先創建一個工程,ipad.web1,編譯運行成功。
- 實現webview的代碼:
#import
@interface ipad_web1ViewController : UIViewController
{
??? IBOutlet UIWebView *myWebView;
}
@property (nonatomic,retain) UIWebView *myWebView;
@end
相應的.m文件:
#import "ipad_web1ViewController.h"
@implementation ipad_web1ViewController
@synthesize myWebView;
- (void)viewDidLoad {
??? [super viewDidLoad];
??? self.myWebView.delegate=self;
????NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
??? [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
??? return YES;
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
}
- (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:@"/click/false"] ) {????
??????? NSLog( @"not clicked" );
??????? return false;
??? }
??? if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/true"] ) {??????? //the image is clicked, variable click is true
??????? NSLog( @"image clicked" );
??????? UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"JavaScript called"
???????????????????????????????????????????????????? message:@"You’ve called iPhone provided control from javascript!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
??????? [alert show];
??????? [alert release];
??????? return false;
??? }
??? return true;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
??? NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
??? NSLog(@"title11=%@",title);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
????NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
??? NSLog(@"title=%@",title);
??? //添加數據
???[myWebView stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('field_2');"??
???? "field.value='Multiple statements - OK';"];
??? //[myWebView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"??
//???? "script.type = 'text/javascript';"??
//???? "script.text = \"function myFunction() { "??
//???? "var field = document.getElementById('field_3');"??
//???? "field.value='Calling function - OK';"??
//???? "}\";"??
//???? "document.getElementsByTagName('head')[0].appendChild(script);"];??
//????
//??? [myWebView stringByEvaluatingJavaScriptFromString:@"myFunction();"];??
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}
@end
- 最后在Interface Builder中添加UIwebView控件,并且和相應的實體相關聯。
???NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
??? NSLog(@"title=%@",title);
主要是獲取html文件的title名字。
[myWebView stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('field_2');"??
???? "field.value='Multiple statements - OK';"];
添加相應的表單信息。
- 接下來添加index.html文件:
ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Segoe UI', Calibri, 'Myriad Pro', Myriad, 'Trebuchet MS', Helvetica, Arial, sans-serif; color: #0071BB; outline-style: none; outline-width: initial; outline-color: initial;">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
???
??? How to build an iPhone website
???
???
???
???
???
???
??? < type="text/javascript" src="test.js">
??
測試
?? click me
?? ??
?????
??
?????
??
?????
??
??? ??
- 添加相應的css文件:
body {
??? background-color: #F2F5A9;
}
- 添加相應的js文件:
function imageClicked(){
??? var clicked=true;
??? window.location="/click/"+clicked;
}
運行,點擊連接應該不出相應的對話框,說明相應的javascript沒有生效。修改辦法是打開targets,點擊ipad.web1,移動相應的test.js文件到下圖即可。
??
源代碼:http://easymorse-iphone.googlecode.com/svn/trunk/ipad.web1/
轉載于:https://www.cnblogs.com/zhuolaiqiang/archive/2011/06/24/2088911.html
總結
以上是生活随笔為你收集整理的ios UIWebView调用本地html和javascript,并且和ios通讯的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】#ifndef作用
- 下一篇: 盘古分词