OS开发之Objective-C与JavaScript交互操作是本文要介绍的内容,主要主要是讲解了stringByEvaluatingJavaScriptFromString的用法,它的功能非常的强大,用起来非常简单,通过它我们可以很方便的操作uiwebview中的页面元素。
UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互。
stringByEvaluatingJavaScriptFromString
使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载完成之后去调用。我们在界面上拖放一个UIWebView控件。在Load中将google mobile加载到这个控件中,代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
webview.backgroundColor = [UIColor clearColor];
webview.scalesPageToFit =YES;
webview.delegate =self;
NSURL *url =[[NSURL alloc]
initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}
我们在webViewDidFinishLoad方法中就可以通过javascript操作界面元素了。
1、获取当前页面的url。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
}
2、获取页面title:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
}
3、修改界面元素的值。
NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='侯文富专栏';"];
4、表单提交:
NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
这样就实现了在google搜索关键字:“侯文富的专栏”的功能。
5、插入js代码
上面的功能我们可以封装到一个js函数中,将这个函数插入到页面上执行,代码如下:
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() { "
"var field = document.getElementsByName('q')[0];"
"field.value='侯文富的专栏';"
"document.forms[0].submit();"
"}
\";"
"document.getElementsByTagName('head')[0].appendChild(script);
"];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction();"
];
看上面的代码:
a、首先通过js创建一个script的标签,type为’text/javascript’。
b、然后在这个标签中插入一段字符串,这段字符串就是一个函数:myFunction,这个函数实现google自动搜索关键字的功能。
c、然后使用stringByEvaluatingJavaScriptFromString执行myFunction函数。
///*//
html 代码
<html>
<head>
<title>HTML中用JS调用OC方法</title>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<script>
function test()
{
alert("test alert...");
return "abcd";
}
</script>
<body>
<br/>
<br/>
<br/>
<a href='ios://openMyAlbum'>打开相机</a><br><br/>
#<a href = 'ios://openMyCamera'>打开相册</a>
</body>
</html>
oc 代码
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlstr = request.URL.absoluteString;
NSRange range = [urlstr rangeOfString:@"ios://"];
if (range.length!=0)
{
NSString *method = [urlstr
substringFromIndex:(range.location+range.length)];
SELselctor = NSSelectorFromString(method);
[self performSelector:selctor withObject:nil];
return NO; //YES 表示正常加载网页 返回NO 将停止网页加载
}
return YES;
}
-(void)openMyAlbum
{
UIImagePickerController
*vc = [[UIImagePickerController alloc]init];
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:vc animated:YES completion:nil];
}
-(void)openMyCamera
{
NSString * strRet = [_webView stringByEvaluatingJavaScriptFromString:@"test();"];
if(strRet isEqualTo:@"abcde")
{
return;
}
UIImagePickerController *vc = [[UIImagePickerControlleralloc]init];
vc.sourceType = UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:vc animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_webView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test.html" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
_webView.delegate = self;
_webView.dataDetectorTypes = UIDataDetectorTypeAll;
[_webView loadRequest:req];
// Do any additional setup after loading the view, typically from a nib.
}
进度条比较好的github开源
NJKWebViewProgress 地址:https://github.com/ninjinkun/NJKWebViewProgress
使用方法地址:http://www.jianshu.com/p/318e774d82fe
NJKWebViewProgressView *_webViewProgressView;
NJKWebViewProgress *_webViewProgress;