很久之前用的互相调用的方法,今天要分享下,故记录下
Objective-C,通常写作ObjC或OC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言。
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。
UIWebView用于在App中嵌入网页内容,通常情况下是html格式的网页,也支持pdf, word等文档。
控件优点
控件缺点
使用场景
排版复杂的内容 需后台灵活控制的界面 原网页
1.加载网页
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
// 获取网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
// 加载请求
[webView loadRequest:request];
[self.view addSubview:webView];
2.js调oc
网页中的点击方法可以调起原生页面
/**
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
NSString *url = [request.URL absoluteString];
if ([url isEqualToString:@"attachment:Upload"]) {
ALCStudentInfoUpLoadViewController *upLoad = [[ALCStudentInfoUpLoadViewController alloc] init];
upLoad.delegate = self;
[self.navigationController pushViewController:upLoad animated:YES];
return NO;
}
return YES;
}
js中写法
<button type="button" onclick="did();">google</button>
function did()
{
window.location.href = 'jsToOC:success';
}
3.oc调js
原生调用js方法,也可把参数传递给js
/**
- (void)didBackButtonWithArray:(NSMutableArray *)array {
NSMutableDictionary *jsParame = [NSMutableDictionary dictionary];
jsParame[@"NewFileList"] = array;
NSString *parame = CodeDictionaryToJsonString(jsParame);
NSString *parameF = [parame stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *parameS = [parameF stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *js = [NSString stringWithFormat:@"cacheAttachMentInfo('%@','%d');", parameS, false];
[self.webView stringByEvaluatingJavaScriptFromString:js];
}
js中写法
function cacheAttachMentInfo(ssss, yyy)
{
alert(ssss);
}