oc与js的交互
1,需求中很多时候需要js调oc和oc调js,js和oc是2种语言,不能直接相互调用
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!request||[[[request URL]absoluteString]length] ==0){
return YES;
}
NSString *requestString = [[[requestURL] absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if([selftryDoNative:requestString]){
return NO;
}
return YES;
}
//自己写的方法
-(BOOL) tryDoNative:(NSString*)requestString{
NSDictionary *param = [selfparseParmaFromUrl:requestString];//解析参数的方法,这个方法需要根据前端的数据类型,设定对应的解析方法
NSString *type = nil;
NSString *orgId = nil;
if(param[@"orgId"]){
orgId = param[@"orgId"];
}
if (param[@"type"]){
type = param[@"type"];
}
if(type && orgId){
self.orgIdFromJS = orgId;
self.delayTarget =1;
if ([typeisEqualToString:JS_ACTION_SHOW_DETAIL])
{
if([selfhasDetail:self.orgIdFromJS])
{
[self enterDetailUI];
}
}else if([typeisEqualToString:JS_ACTION_ATTENTION])
{
[[OrganizationServicesharedInstance]subscribeOrganization:_model.orgIdcallback:self];
}
return YES;
}
return NO;
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self registJSMethod:@"iosAction" block:^(NSArray *params) {
dispatch_async(dispatch_get_main_queue(), ^{
[self iosAction:params];
});
}];
}
//先注册和前端约定的方法名 当点击网页按钮会执行对应的方法名对应的block
-(void)registJSMethod:(NSString*)name block:(void(^)(NSArray*))block
{
JSContext *context=[self.webVie valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[name] = ^() {
NSArray *args = [JSContextcurrentArguments];
dispatch_async(dispatch_get_main_queue(), ^{
block(args);
});
};
}
-(void)iosAction:(NSArray*)params{
//经过解析参数 根据参数 调原声的方法
JSValue *v2 = params[1];//取出来的参数是JSValue型的 需要转换成oc类型的
JSValue *v1 = params[0];
NSString *u = [v2toString];
NSString *state = [v1 toString];
NSDictionary *dic = [uobjectFromJSONString];
if([state isEqualToString:@"6"]){
//调原生1
}else if([stateisEqualToString:@"12"]){
//调原生2
}else if ([stateisEqualToString:JS_ACTION_VISTOR]){
//调原生3
}
}
//当要调js的代码时 先调用这个方法
-(void)callJsInitializationPraiseTap:(long )praiseInt //自己写的 不要认为是系统的方法
{
NSDictionary *dic = @{@"flag":@(praiseInt)};
[selfcallJSMethod:dicindex:JS_INIT_PRASE];
}
-(id)callJSMethod:(NSMutableDictionary *)dic index:(NSInteger)index{
NSString *str1 = [dic JSONString];
NSString *str = [str1stringByReplacingOccurrencesOfString:@"\""withString:@"'"];
NSString *method = [NSStringstringWithFormat:@"Initialization(%ld,\"%@\")",(long)index,str];
//参数@"Initialization(%ld,\"%@\")" 是和前端约定的
return [selfcallJSMethod:method];
}
-(id)callJSMethod:(NSString*)method
{
JSContext *context=[self.webViewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *jsmethod =method; //准备执行的js代码
JSValue *value = [context evaluateScript:jsmethod];//通过oc方法调用js的alert
return [value toObject];
}
//当要调js的代码时 先调用这个方法
-(void)callJsInitializationPraiseTap:(long )praiseInt //自己写的 不要认为是系统的方法
{
NSDictionary *dic = @{@"flag":@(praiseInt)};
[selfcallJSMethod:dic index:JS_INIT_PRASE];
}
-(id)callJSMethod:(NSMutableDictionary *)dic index:(NSInteger)index{
NSString *str1 = [dic JSONString];
NSString *str = [str1stringByReplacingOccurrencesOfString:@"\""withString:@"'"];
NSString *method = [NSStringstringWithFormat:@"Initialization(%ld,\"%@\")",(long)index,str];
//参数@"Initialization(%ld,\"%@\")" 是和前端约定的
[_webViewstringByEvaluatingJavaScriptFromString:method];