@implementation ViewController
{
UIButton *btn;
UIWebView *webview;
BOOL isToNext;
}
- (void)viewDidLoad {
[super viewDidLoad];
isToNext = NO;
[self creatUI];
}
-(void)creatUI{
webview = [[UIWebViewalloc]initWithFrame:self.view.bounds];
webview.backgroundColor = [UIColoryellowColor];
webview.delegate =self;
[self.viewaddSubview:webview];
NSURL *url = [[NSBundle mainBundle]URLForResource:@"Test" withExtension:@"html"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[webview loadRequest:request];
UIButton *btn1 =[UIButtonbuttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0,500,200,40);
btn1.backgroundColor = [UIColorblueColor];
[btn1 setTitle:@"oc调用无参数js"forState:UIControlStateNormal];
[btn1 addTarget:selfaction:@selector(btn1click)forControlEvents:UIControlEventTouchUpInside];
UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(0,580,200,40);
btn2.backgroundColor = [UIColorblueColor];
[btn2 setTitle:@"oc调用有参数js"forState:UIControlStateNormal];
[btn2 addTarget:selfaction:@selector(btn2click)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn1];
[self.viewaddSubview:btn2];
}
#pragma MARK ---oc->js
-(void)btn1click{
[webview stringByEvaluatingJavaScriptFromString:@"buttonclick()"];
}
-(void)btn2click{
NSString *name = @"Timer";
NSString *number =@"111111";
NSString *str = [NSStringstringWithFormat:@"buttonclick1('%@','%@')",name,number];
[webview stringByEvaluatingJavaScriptFromString:str];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// 开始相应请求触发
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
// 开始加载网页
}
#pragma mark:-------js->oc
-(void)webViewDidFinishLoad:(UIWebView *)webView{
// 向js中插入一段代码
NSString *str = @"document.getElementById('AddPfromOC').innerHTML = '这段文字是由oc加入的js代码,在网页加载完成的时候加入';";
[webview stringByEvaluatingJavaScriptFromString:str];
// 加载完毕
_context = [webviewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
_context[@"jscall1"] = ^(){
[self changenav];
};
_context[@"jscall2"] = ^(){
// 这里需要对数据操作
NSArray *args = [JSContextcurrentArguments];
NSString *name = args[0];
NSString *num = args[1];
[self changenavwith:nameand:num];
};
_context[@"jscall3"] = ^(){
// 这个地方取到的值是jsvalue 而不是nsstring如果需要做判断需要进行转类型上面是错的但是我懒
NSArray *args = [JSContextcurrentArguments];
JSValue *block = args[2];
NSString *str = block.toString;
[self isOKwith:str];
};
}
-(void)isOKwith:(NSString *)block{
if ([block isEqualToString:@"bolck"]) {
[self button3success:block];
}else{
[self button3error:block];
}
}
// 这个就当做是筛选吧 一个回调
-(void)button3success:(NSString *)block{
NSString *str = [NSStringstringWithFormat:@"jscallblock('校验成功%@')",block];
[webview stringByEvaluatingJavaScriptFromString:str];
}
-(void)button3error:(NSString *)block{
NSString *str = [NSStringstringWithFormat:@"jscallblock('校验失败%@')",block];
[webview stringByEvaluatingJavaScriptFromString:str];
}
-(void)changenav{
self.navigationItem.title =@"无参数方法调用";
}
-(void)changenavwith:(NSString *)name and:(NSString *)number{
self.navigationItem.title =[NSStringstringWithFormat:@"姓名%@,密码%@",name,number];
}
关于: webView---JS和Swift交互,
WkWebView----JS和Swift交互,
wkWebView ---- JS和OC交互
可以询问我demo