iOS:在OC中调用JS脚本

时间:2024-04-03 16:36:45

示例一:在webView中调用js脚本进行搜索

1.首先导入JavaScriptCore.framework这个框架

iOS:在OC中调用JS脚本

2.创建webView、设置代理、请求手机端百度

iOS:在OC中调用JS脚本
#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIWebViewDelegate>
@property (strong,nonatomic)UIWebView *webView;
@property (assign,nonatomic)BOOL isFirstLoadWeb;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    NSURL *URL = [NSURL URLWithString:@"http://m.baidu.com"];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:URL];
    [self.webView loadRequest:request];
}
iOS:在OC中调用JS脚本

3.通过代理方法,在里面调用自定义的JS脚本

iOS:在OC中调用JS脚本
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    //程序会一直调用该方法,所以判断若是第一次加载后使用我们自定义的js,此后不再调用JS,否则会出现网页抖动现象
    if (!_isFirstLoadWeb) {
        _isFirstLoadWeb = YES;
        
        //获取当前页面的url
        NSString *url = [webView  stringByEvaluatingJavaScriptFromString:@"document.location.href"];
        //获取页面的title
        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        
        NSLog(@"%@----%@",url,title);
        
        //给webView添加一个自定义的JavaScript
        [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
            "script.type = 'text/javascript';"
            "script.text = \"function myFuncation(){"
            "var field = document.getElementsByName('word')[0];"
            "field.value = '夏先森的博客园';"
            "document.forms[0].submit();"
         "}\";"
         "document.getElementsByTagName('head')[0].appendChild(script);"];
        
        //开始调用自定义的JavaScript
        [webView stringByEvaluatingJavaScriptFromString:@"myFuncation();"];

    }else{
        return;
    }
}

@end
iOS:在OC中调用JS脚本

演示截图如下:

2016-04-18 21:37:24.670 OC调用JS[7568:356964] https://m.baidu.com/----百度一下

iOS:在OC中调用JS脚本  iOS:在OC中调用JS脚本

 

 

示例二:通过滑动条动态改变网页中字体的大小

1.首先导入JavaScriptCore.framework这个框架

iOS:在OC中调用JS脚本

2.创建webView、设置代理、请求手机端百度

iOS:在OC中调用JS脚本
#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIWebViewDelegate>
@property (strong,nonatomic)UIWebView *webView;
@property (strong,nonatomic)UIActivityIndicatorView *activityIndicator;
@property (strong,nonatomic)UISlider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建slider
    _slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH-20, 20)];
    [_slider addTarget:self action:@selector(SliderChanged) forControlEvents:UIControlEventValueChanged];
    _slider.maximumValue = 1000.0f;
    _slider.minimumValue = 10.0f;
    _slider.value = 10.0f;
    [self.view addSubview:_slider];
    
    //2.创建webView
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, SCREEN_HEIGHT-80)];
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    NSURL *URL = [NSURL URLWithString:@"http://m.baidu.com"];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:URL];
    [self.webView loadRequest:request];
    
    //3.创建活动指示器
    _activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 40, 50)];
    _activityIndicator.center = self.view.center;
    _activityIndicator.backgroundColor = [UIColor grayColor];
    _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [self.view addSubview:_activityIndicator];
}
iOS:在OC中调用JS脚本

3.滑动滑动条来改变网页字体大小

iOS:在OC中调用JS脚本
-(void)SliderChanged{
    NSString *str1 = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust = '%f%%'",_slider.value];
    
    [_webView stringByEvaluatingJavaScriptFromString:str1];
}
@end
iOS:在OC中调用JS脚本

演示截图如下:

iOS:在OC中调用JS脚本  iOS:在OC中调用JS脚本

iOS:在OC中调用JS脚本

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5405997.html,如需转载请自行联系原作者