1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OCJS互调</title>
</head>
<body>
周杰伦
110
<button style="background:red; width:100px; height: 30px" onclick="jsCallOc()">
Call
</button>
<br>
<a href="https://www.baidu.com/">百度一下就知道了</a>
</body>
<script type="text/javascript">
function ocCallJsNoParams() {
alert("oc调用js无参数");
}
function ocCallJsHsParams(name , address){
alert(name+"的地址是:"+address);
}
function jsCallOc(){
location.href = 'InternetProtocol://Parameter1_withParameter2_?name&age';
}
</script>
</html>
用WebView加载以上的静态网页。
2.
#import "NSObject+JSCallOC.h"
@implementation NSObject (JSCallOC)
-(id)t_performSelector:(SEL)aSelector withObject:(NSArray *)objects{
// signature 记录方法的返回值和参数类型
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:aSelector];
if (signature == nil) {
// 签名没有抛出异常
NSString *reason = [NSString stringWithFormat:@"没有%@的方法",NSStringFromSelector(aSelector)];
NSException *exception = [NSException exceptionWithName:@"错误" reason:reason userInfo:nil];
@throw exception;
}
// NSInvocation 包装了一次方法的调用(方法的调用者,方法名, 参数 , 返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;//指定方法的调用
invocation.selector = aSelector;//指定方法名
// 设置参数
NSInteger parameterCount = signature.numberOfArguments -2;
parameterCount = MIN(parameterCount, objects.count);
for (int i = 0 ; i < parameterCount; i ++) {
id param = objects[i];
// 剔除空参数
if (![param isKindOfClass:[NSNull class]]) {
[invocation setArgument:¶m atIndex:i +2];
}
}
// 调用
[invocation invoke];
// 设置方法的返回值
id returnValue = nil;
if (signature.methodReturnLength) {
[invocation getReturnValue:&returnValue];
}
return returnValue;
}
3.
#import "ViewController.h"
#import "NSObject+JSCallOC.h"
@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"ocjs.html" withExtension:nil]]];
self.webView.delegate = self;
}
#pragma mark -- webViewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//获取url全路径
NSString *url = request.URL.absoluteString;
NSString *protocol = @"internetprotocol://";
if ([url hasPrefix:protocol]) {
//获取协议后的路径
NSString *path = [url substringFromIndex:protocol.length];
//利用占位符分割方法名 和 参数
NSArray *subPath = [path componentsSeparatedByString:@"?"];
//获取方法名
NSString *methodName = [subPath.firstObject stringByReplacingOccurrencesOfString:@"_" withString:@":"];
//获取参数
NSArray *params = nil;
if (subPath.count == 2) {
params = [subPath.lastObject componentsSeparatedByString:@"&"];
}
SEL sel = sel_registerName([methodName UTF8String]);
//调用方法
[self t_performSelector:sel withObject:params];
return NO;
}
return YES;
}
//oc -----> js
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *javaScript = @"ocCallJsNoParams()";
// NSString *javaScript = [NSString stringWithFormat:@"ocCallJsHsParams('%@' , '%@')",@"无限",@"WIFI"];
[webView stringByEvaluatingJavaScriptFromString:javaScript];
}
//js --- >oc
-(void)call:(NSString *)name{
NSLog(@"参数是:%@",name);
NSLog(@"%s",__FUNCTION__);
}
-(void)Parameter1:(NSString *)parameter1 withParameter2:(NSString *)parameter2{
NSLog(@"%@---%@",parameter1,parameter2);
}