UIWebView与js交互(一)
2015-08-17 15:07
531 查看
UIWebView与js交互
UIWebView与js交互,关键在于stringByEvaluatingJavaScriptFromString;它可以实现从js获取title等内容还可以给js发送数据。
具体如下:
- (void)webViewDidFinishLoad:(UIWebView *)webView;在webViewDidFinishLoad方法里面,通过如下方式可以获取你需要的内容:
1、获取当前网页的网址
//获取当前网页的网址
NSString * currentUrl = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"currentUrl:%@",currentUrl);
2、获取当前网页的title
//获取当前网页的title
NSString * currentUrlTitle = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"currentUrlTitle:%@",currentUrlTitle);
3、获取整个html页面的js代码
//整个html页面的js代码
NSString * allHTML = [ webViewstringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"someHTML:%@",allHTML);
第一步:
UIWebView * myWebView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(myTextField.frame)+leftSpace,SCREEN_WIDTH,SCREEN_HEIGHT-CGRectGetMaxY(myTextField.frame)+leftSpace)];
myWebView.backgroundColor = [UIColorgrayColor];
myWebView.scalesPageToFit =YES;
myWebView.delegate =self;
[self.viewaddSubview:myWebView];
self.myWebView = myWebView;
[selfrequestUrl];
第二步:
-(void)requestUrl
{
[selfshouldresignFirstResponder];
NSURLRequest * request = [NSURLRequestrequestWithURL:[NSURLURLWithString:self.myTextField.text]];
[self.myWebViewloadRequest:request];
}
第三步:
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"1::shouldStartLoadWithRequest");
return
YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"2::webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"3::webViewDidFinishLoad");
//获取当前网页的网址
NSString * currentUrl = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"currentUrl:%@",currentUrl);
self.myTextField.text = currentUrl;
//获取当前网页的title
NSString * currentUrlTitle = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"currentUrlTitle:%@",currentUrlTitle);
self.title = currentUrlTitle;
//整个html页面的js代码
NSString * allHTML = [ webViewstringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"someHTML:%@",allHTML);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"4::didFailLoadWithError");
}
UIWebView与js交互,关键在于stringByEvaluatingJavaScriptFromString;它可以实现从js获取title等内容还可以给js发送数据。
具体如下:
- (void)webViewDidFinishLoad:(UIWebView *)webView;在webViewDidFinishLoad方法里面,通过如下方式可以获取你需要的内容:
1、获取当前网页的网址
//获取当前网页的网址
NSString * currentUrl = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"currentUrl:%@",currentUrl);
2、获取当前网页的title
//获取当前网页的title
NSString * currentUrlTitle = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"currentUrlTitle:%@",currentUrlTitle);
3、获取整个html页面的js代码
//整个html页面的js代码
NSString * allHTML = [ webViewstringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"someHTML:%@",allHTML);
第一步:
UIWebView * myWebView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(myTextField.frame)+leftSpace,SCREEN_WIDTH,SCREEN_HEIGHT-CGRectGetMaxY(myTextField.frame)+leftSpace)];
myWebView.backgroundColor = [UIColorgrayColor];
myWebView.scalesPageToFit =YES;
myWebView.delegate =self;
[self.viewaddSubview:myWebView];
self.myWebView = myWebView;
[selfrequestUrl];
第二步:
-(void)requestUrl
{
[selfshouldresignFirstResponder];
NSURLRequest * request = [NSURLRequestrequestWithURL:[NSURLURLWithString:self.myTextField.text]];
[self.myWebViewloadRequest:request];
}
第三步:
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"1::shouldStartLoadWithRequest");
return
YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"2::webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"3::webViewDidFinishLoad");
//获取当前网页的网址
NSString * currentUrl = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"currentUrl:%@",currentUrl);
self.myTextField.text = currentUrl;
//获取当前网页的title
NSString * currentUrlTitle = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"currentUrlTitle:%@",currentUrlTitle);
self.title = currentUrlTitle;
//整个html页面的js代码
NSString * allHTML = [ webViewstringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"someHTML:%@",allHTML);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"4::didFailLoadWithError");
}
相关文章推荐
- androidのstring.xml转译、特殊字符问题处理
- 30多个iOS常用动画,带详细注释
- Unity打包android的apk与数据包.obb分离和apk签名
- Android中几种图像特效处理的集锦!!
- [iOS]调用系统相机,相册,闪光灯
- iOS编程——Swift语法之 "?" 和 "!"
- [转]cocos2dx中用外部浏览器打开url
- Cocos2d-x能够实现的效果总结
- Unity3d的单例及场景间的数据传递
- Objective-C 数据类型定义的标示符(一)
- android 实现真正意义上的服务及源代码下载
- Swift版 ScrollView和UITouch事件冲突
- Android WebView 因重定向无法正常goBack()解决方案
- iOS自动检查更新
- Android 开发之 SimpleTagImageView自定义ImageView
- iOS开发系列--地图与定位
- 安卓学习笔记(一)自定义控件2
- CF_462B_ApplemanAndCardGame
- iOS颜色16进制值转换RGB
- 慕课Swift-进入OOP时代笔记