1.js调用oc代码 oc中的网页html
<!DOCTYPE html>
<html>
<head lang="en">
<metacharset="UTF-8">
<title></title>
<style>
body{
margin:40px;
}
</style>
</head>
<body>
<buttonοnclick="openCamera();">访问相册</button>
<scripttype="text/javascript">
function openCamera(){
window.location.href ='dswq:///openCamera';
}
</script>
</body>
</html>
oc的代码
<!DOCTYPE html>
<html>
<head lang="en">
<metacharset="UTF-8">
<title></title>
<style>
body{
margin:40px;
}
</style>
</head>
<body>
<buttonοnclick="openCamera();">访问相册</button>
<scripttype="text/javascript">
function openCamera(){
window.location.href ='dswq:///openCamera';
}
</script>
</body>
</html>
- (void)viewDidLoad {
[superviewDidLoad];
NSURL *url = [[NSBundlemainBundle] URLForResource:@"test"withExtension:@"html"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[self.webViewloadRequest:request];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"%@", request.URL.absoluteString);
NSString *str = request.URL.absoluteString;
NSRange range = [strrangeOfString:@"dswq:///"];
NSUInteger location = range.location;
if (location !=NSNotFound) {
NSString *method = [strsubstringFromIndex:location + range.length];
SEL sel =NSSelectorFromString(method);
[selfperformSelector:sel];
}
returnYES;
}
//实现访问相册
- (void)openCamera{
UIImagePickerController *pickerVC = [[UIImagePickerControlleralloc] init];
pickerVC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:pickerVC animated:YEScompletion:nil];
}
- (void)viewDidLoad {
[superviewDidLoad];
NSURL *url = [[NSBundlemainBundle] URLForResource:@"test"withExtension:@"html"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[self.webViewloadRequest:request];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"%@", request.URL.absoluteString);
NSString *str = request.URL.absoluteString;
NSRange range = [strrangeOfString:@"dswq:///"];
NSUInteger location = range.location;
if (location !=NSNotFound) {
NSString *method = [strsubstringFromIndex:location + range.length];
SEL sel =NSSelectorFromString(method);
[selfperformSelector:sel];
}
returnYES;
}
//实现访问相册
- (void)openCamera{
UIImagePickerController *pickerVC = [[UIImagePickerControlleralloc] init];
pickerVC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:pickerVC animated:YEScompletion:nil];
}
2.oc调用js代码
- (void)viewDidLoad {
[superviewDidLoad];
self.webView.scrollView.hidden =YES;
self.webView.backgroundColor = [UIColorgrayColor];
// 加载一个网页
NSURL *url = [NSURLURLWithString:@"http://www.xianhua.cn/m/"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[self.webViewloadRequest:request];
}
#pragma mark - <UIWebViewDelegate>
-(void)webViewDidFinishLoad:(UIWebView *)webView{
// 删除底部的广告
NSString *str = @" document.getElementsByClassName('detail_btns2')[0].remove();";
[webViewstringByEvaluatingJavaScriptFromString:str];
// 修改标题
NSString *str2 = @"document.getElementsByTagName('h1')[0].innerText = '--鲜花网--';";
[webViewstringByEvaluatingJavaScriptFromString:str2];
// 修改底部的标题
NSString *str3 = @"document.getElementById('xiazaiapp').getElementsByTagName('a')[0].innerText = '--下载鲜花网APP--';";
[webViewstringByEvaluatingJavaScriptFromString:str3];
}