zoukankan      html  css  js  c++  java
  • js调用oc和oc调用js

    一、js调用oc

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"test" withExtension:@"html"];
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
        self.webview.delegate=self;
        [self.webview loadRequest:request];
        
    }
    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSLog(@"%@",request.URL.absoluteString);
        NSString *str=request.URL.absoluteString;
        NSRange range=[str rangeOfString:@"dswq:///"];
        NSUInteger location=range.location;
        if (location != NSNotFound) {
            NSString *method=[str substringFromIndex:location+range.length];
            SEL sel=NSSelectorFromString(method);
            [self performSelector:sel];
        }
        return YES;
    }
    -(void)openCamera{
        UIImagePickerController *pickerVC=[[UIImagePickerController alloc]init];
        pickerVC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

     二、oc调用js

    - (void)viewDidLoad {
        [superviewDidLoad];
        
       self.webView.scrollView.hidden =YES;
       self.webView.backgroundColor = [UIColorgrayColor];
        
       // 加载一个网页
       NSURL *url = [NSURLURLWithString:@"http://www.xianhua.cn/m/"];
       NSURLRequest *request = [NSURLRequestrequestWithURL:url];
        [self.webViewloadRequest:request];
    }
    #pragma mark - <UIWebViewDelegate>
    -(void)webViewDidFinishLoad:(UIWebView *)webView{
       // 删除底部的广告
       NSString *str = @" document.getElementsByClassName('detail_btns2')[0].remove();";
        [webViewstringByEvaluatingJavaScriptFromString:str];
        
       // 修改标题
       NSString *str2 = @"document.getElementsByTagName('h1')[0].innerText = '--鲜花网--';";
        [webViewstringByEvaluatingJavaScriptFromString:str2];
        
       // 修改底部的标题
       NSString *str3 = @"document.getElementById('xiazaiapp').getElementsByTagName('a')[0].innerText = '--下载鲜花网APP--';";
        [webViewstringByEvaluatingJavaScriptFromString:str3];
    }



    转载:http://blog.csdn.net/wzios/article/details/61202082

     

  • 相关阅读:
    Python基础(一)
    计算机编程和编程语言
    初始Markdown
    Python模块
    Python递归以及面向过程编程
    Python推导式和匿名函数
    Python学闭包函数和装饰器
    Python函数的特点
    Python文件高级应用和如何使用函数
    Python字符编码和文件处理
  • 原文地址:https://www.cnblogs.com/y16879w/p/6943988.html
Copyright © 2011-2022 走看看