码迷,mamicode.com
首页 > 移动开发 > 详细

iOS web与js的简单交互

时间:2016-04-27 09:31:32      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

我们在封装网页的时候经常会遇到需要往网页里面的控件添加数据,但是怎么添加又成了难点。本人最近在开发的时候就遇到这样的事,解决之后,来和大家分享一下。

 

//以必应网站为例

[web loadRequest:

[NSURLRequestrequestWithURL:

[NSURL URLWithString:@"http://www.bing.com/?FORM=Z9FD1"]]];

 

以下就是主要代码,只有几句

 

//获取当前页面的title

NSString *title =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.title"];

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

//获取当前URL

NSString *URL =

[webView

stringByEvaluatingJavaScriptFromString: @"document.location.href"];

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

//得到网页代码

NSString *html =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.documentElement.innerHTML" ];

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

//拼接字符串 根据网页name找到控价并赋值

NSString *str = @"随_的简书";

NSString *JSStr =

[NSString stringWithFormat:

@"document.getElementsByName(‘q‘)[0].value = (‘%@‘);",str];

[webView stringByEvaluatingJavaScriptFromString: JSStr];

 

以下是全部代码,commit+c commit+v可以直接运行

 

@interface ViewController ()

@property (weak, nonatomic) UIWebView *web;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIWebView *web =

[[UIWebView alloc] initWithFrame:

[UIScreen mainScreen].bounds];

UIButton *back =

[[UIButton alloc] initWithFrame:CGRectMake

(0, 0, self.view.frame.size.width,self.view.frame.size.width / 7.5)];

[back setBackgroundColor: [UIColor orangeColor]];

[back setTitle:@"back" forState:UIControlStateNormal];

[back addTarget:self

action: @selector(back)

forControlEvents:UIControlEventTouchUpInside];

//以必应为例

[web loadRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:

@"http://www.bing.com/?FORM=Z9FD1"]]];

web.delegate = self;

web.scalesPageToFit =YES;

web.scrollView.delegate = self;

self.web = web;

[self.view addSubview:web];

[self.view addSubview:back];

}

#pragma mark ---Delegate

-(void) webViewDidStartLoad:

(UIWebView *)webView{

NSLog(@"开始加载---") ;

}

- (void) webViewDidFinishLoad:

(UIWebView *)webView {

NSLog(@"加载完成---");

//获取当前页面的title

NSString *title =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.title"];

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

//获取当前URL

NSString *URL =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.location.href"];

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

//得到网页代码

NSString *html =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.documentElement.innerHTML" ];

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

//拼接字符串 根据网页name找到控价并赋值

NSString *str = @"随_的简书";

NSString *JSStr =

[NSString stringWithFormat:

@"document.getElementsByName(‘q‘)[0].value = (‘%@‘);",str];

[webView stringByEvaluatingJavaScriptFromString:JSStr];

}

- (void) webView:

(UIWebView *)webView

didFailLoadWithError:(NSError *)error {

NSLog(@"加载失败===%@",error);

}

//当网页位置为顶部 不允许继续下拉

- (void) scrollViewDidScroll:

(UIScrollView *)scrollView {

if (self.web.frame.origin.y == 0) {

self.web.scrollView.bounces = NO;

return;

}

}

//webView的每次页面跳转都会执行,在这里可以得到想要的数据

- (BOOL)webView:

(UIWebView *)webView

shouldStartLoadWithRequest:(NSURLRequest *)request

navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"页面跳转");

return YES;

}

//返回

- (void) back {

[self.web goBack];

}

 

iOS web与js的简单交互

标签:

原文地址:http://www.cnblogs.com/fengmin/p/5437466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!