• iOS web与js的简单交互


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

    //以必应网站为例

    [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];

    }

  • 相关阅读:
    2019-06-09 学习日记 day30 JS
    2019-06-08 学习日记 day29 CSS
    2019-06-07 学习日记 day28 THML
    2019-06-06 Java学习日记 day27 反射
    2019-06-05 Java学习日记 day26 网络编程
    2019-06-04 Java学习日记 day25 多线程下
    Linux安装Nginx
    Linux安装MySQL
    Linux安装Redis
    Java Swing实战(五)表格组件JTable(1)
  • 原文地址:https://www.cnblogs.com/fengmin/p/5437466.html
Copyright © 2020-2023  润新知