html代码:
oc代码:
js调oc:
https://github.com/mcmengchen/iOS---MIthcell-NSInvocation/blob/master/Mitchell%20-%20NSInvocation/Mitchell%20-%20NSInvocation/NSObject%2BperformSelector.m
作者:Mitchell
链接:http://www.jianshu.com/p/eddb140f3311
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Mitchell</title>
<script type="text/javascript">
function show() { alert(1); }
function showTittle() { alert(document.title); }
function aaa() { location.href="https://www.baidu.com/"; }
function btnClick(){ location.href="mitchell://call"; }
function btnClickTwo(){ location.href="mitchell://callWithNumber_?10086"; }
function btnClickThree(){ location.href="mitchell://sendMessageWithNumber_WithContent_?10086&love"; } </script>
</head>
<body> <style type="text/css"> .test { color: blue; } #op { color: orange; } .outer table, .pp { color: green; } input[type=text] { color: yellow; } </style> <div class="outer"> <label for=“username”>姓名</label> <button style="background:red; height:150px; weight:150px;" οnclick="btnClickThree();">哥是按钮</button> </div> </body></html>
- (void)viewDidLoad { [super viewDidLoad]; NSURL*url = [[NSBundle mainBundle] URLForResource:@"test.html" withExtension:nil]; NSURLRequest*request = [NSURLRequest requestWithURL:url]; self.webView.delegate =self; [self.webView loadRequest:request];}-(void)webViewDidFinishLoad:(UIWebView *)webView{
//OC调用JS,只要利用UIWebView的stringByEvaluatingJavaScriptFromString方法,告诉系统
[self.webView stringByEvaluatingJavaScriptFromString:@"show();"];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL*url = [[NSBundle mainBundle] URLForResource:@"test.html" withExtension:nil];
NSURLRequest*request = [NSURLRequest requestWithURL:url];
self.webView.delegate =self; [self.webView loadRequest:request];
}
//每次请求都会调用//利用该方法作为JS和OC之间的桥梁
//JS跳转网页
//在OC代理方法中通过判断自定义协议头,决定是否是JS调用OC方法
//在OC代理方法中通过截取字符串,获取JS想调用的OC方法名称
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
// NSLog(@"%@",request.URL);
//JS调用OC 无参数
// NSString*schem = @"mitchell://";
// NSString*urlStr = request.URL.absoluteString;
// if ([urlStr hasPrefix:schem]) {// NSLog(@"调用OC方法");
//
//1、从URL汇总获取方法名称
// //Mitchell://call
// NSString*methodName = [urlStr substringFromIndex:schem.length];
// NSLog(@"%@",methodName);
// //2、调用方法// SEL sel = NSSelectorFromString(methodName);
// //下面这一行代码是用于指定忽略警告信息
// //忽略警告信息的作用开始//#pragma clang diagnostic push
// //忽略的警告信息//#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
// [self performSelector:sel withObject:nil];//
//忽略警告信息的作用结束//#pragma clang diagnostic pop return NO;// }
//1个参数// NSString*schem = @"mitchell://";// NSString*urlStr = request.URL.absoluteString;// if ([urlStr hasPrefix:schem]) {// NSLog(@"调用OC方法");// //1、从URL汇总获取方法名称// NSString*subPath = [urlStr substringFromIndex:schem.length];// //注意:如果制定的用于切割字符串不存在,就会返回整个字符串// NSArray*subPaths = [subPath componentsSeparatedByString:@"?"];// //2、获取方法名称// NSString*methodName = [subPaths firstObject];// methodName = [methodName stringByReplacingOccurrencesOfString:@"_" withString:@":"];// NSLog(@"%@",methodName);// //3、调用方法// SEL sel = NSSelectorFromString(methodName);// NSString*params = nil;// if (subPaths.count ==2) {// params = [subPaths lastObject];// }// [self performSelector:sel withObject:params];// return NO;// } //2个参数// NSString*schem = @"mitchell://";// NSString*urlStr = request.URL.absoluteString;// if ([urlStr hasPrefix:schem]) {// NSLog(@"调用OC方法");// //1、从URL汇总获取方法名称// NSString*subPath = [urlStr substringFromIndex:schem.length];// //注意:如果制定的用于切割字符串不存在,就会返回整个字符串// //sendMessageWithNumber_WithContent_?10086&love// NSArray*subPaths = [subPath componentsSeparatedByString:@"?"];// //2、获取方法名称// NSString*methodName = [subPaths firstObject];// methodName = [methodName stringByReplacingOccurrencesOfString:@"_" withString:@":"];// //3、调用方法// SEL sel = NSSelectorFromString(methodName);// NSString*param = nil;// if (subPaths.count ==2) {// param = [subPaths lastObject];// //3、截取参数// NSArray*params = [param componentsSeparatedByString:@"&"];// [self performSelector:sel withObject:[params firstObject] withObject:[params lastObject]];// return NO;// }// [self performSelector:sel withObject:param];// return NO;// }
//多个参数,这里使用了用NSInvocation封装的一个类
NSString*schem = @"mitchell://";
NSString*urlStr = request.URL.absoluteString;
if ([urlStr hasPrefix:schem]) { NSLog(@"调用OC方法");
//1、从URL汇总获取方法名称
NSString*subPath = [urlStr substringFromIndex:schem.length];
//注意:如果制定的用于切割字符串不存在,就会返回整个字符串
//sendMessageWithNumber_WithContent_?10086&love
NSArray*subPaths = [subPath componentsSeparatedByString:@"?"];
//2、获取方法名称
NSString*methodName = [subPaths firstObject];
methodName = [methodName stringByReplacingOccurrencesOfString:@"_" withString:@":"];
//3、调用方法 SEL sel = NSSelectorFromString(methodName); NSArray *params = nil; if (subPaths.count ==2) {
//3、截取参数 params = [[subPaths lastObject] componentsSeparatedByString:@"&"];
[self performSelector:sel withObjects:params]; return NO;
}
[self performSelector:sel withObjects:params];
return NO;
}
return YES;
}
https://github.com/mcmengchen/iOS---MIthcell-NSInvocation/blob/master/Mitchell%20-%20NSInvocation/Mitchell%20-%20NSInvocation/NSObject%2BperformSelector.m
作者:Mitchell
链接:http://www.jianshu.com/p/eddb140f3311
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。