1.在iOS中调用HTML
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载网页
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#pragma mark - 操作网页
-(void)webViewDidFinishLoad:(UIWebView *)webView{
// 删除
NSString *str1 = @"var word = document.getElementById('word');";
NSString *str2 = @"word.remove();";
[webView stringByEvaluatingJavaScriptFromString:str1];
[webView stringByEvaluatingJavaScriptFromString:str2];
// 更改
NSString *str3 = @"var change = document.getElementsByClassName('change')[0];"
"change.innerHTML = '好你的哦!';";
[webView stringByEvaluatingJavaScriptFromString:str3];
// 插入
NSString *str4 =@"var img = document.createElement('img');"
"img.src = 'img_01.jpg';"
"img.width = '160';"
"img.height = '80';"
"document.body.appendChild(img);";
[webView stringByEvaluatingJavaScriptFromString:str4];
}
@end
index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p id="word">12122121212112</p>
<ul>
<li class="change">你好</li>
<li>我好</li>
<li>大家好</li>
<li>才是真的好</li>
</ul>
</body>
</html>
2.ios调用HTML2
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
/** <#注释#> */
@property (nonatomic, weak) UIActivityIndicatorView *activityView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载网页
NSURL *url = [[NSBundle mainBundle] URLForResource:@"xianhua" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
// self.webView.scrollView.hidden = YES;
// self.webView.backgroundColor = [UIColor grayColor];
//
// UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// // 转起来
// [activityView startAnimating];
// activityView.center = self.view.center;
// [self.webView addSubview:activityView];
// self.activityView = activityView;
}
#pragma mark - 操作网页
-(void)webViewDidFinishLoad:(UIWebView *)webView{
// // 改变标题
// NSString *str1 = @"var h1 = document.getElementsByTagName('h1')[0];"
// "h1.innerHTML='大神一期鲜花网';";
// [webView stringByEvaluatingJavaScriptFromString:str1];
//
// //删除尾部
// NSString *str2 =@"document.getElementById('footer').remove();";
// [webView stringByEvaluatingJavaScriptFromString:str2];
//
// // 让scrollView显示
// self.webView.scrollView.hidden = NO;
// self.activityView.hidden = YES;
//
// // 拿出所有的网页内容
// NSString *str3 = @"document.body.outerHTML";
// NSString *html = [webView stringByEvaluatingJavaScriptFromString:str3];
// NSLog(@"%@", html);
}
@end
3.在HTML中调用OC
index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<button onclick="getImage();">访问相册</button>
<script>
function getImage(){
window.location.href = 'xmg://getImage';
}
</script>
</body>
</html>
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate, UINavigationControllerDelegate, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载网页
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"xmg://"];
if (range.location != NSNotFound) {
NSString *method = [str substringFromIndex:range.location + range.length];
SEL sel = NSSelectorFromString(method);
[self performSelector:sel];
}
// NSLog(@"%@", str);
return YES;
}
// 访问相册
- (void)getImage{
UIImagePickerController *pickerImg = [[UIImagePickerController alloc]init];
pickerImg.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerImg animated:YES completion:nil];
}
@end
4.在HTML中调用OC
<html>
<head>
<meta charset="UTF-8">
<style>
body{
margin:20px;
}
</style>
</head>
<body>
<p></p>
<span>用户名</span>
<input id="username">
<p></p>
<span>密码</span>
<input id="pwd" type="password">
<p></p>
<button id="btn">登录</button>
<script>
var username = document.getElementById('username');
var pwd = document.getElementById('pwd');
var btn = document.getElementById('btn');
btn.onclick = function(){
var name = username.value; // 拿到用户名
var password = pwd.value;
window.location.href = 'xmg://' + name + '?' + password;
}
</script>
</body>
</html>
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate, UINavigationControllerDelegate, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载网页
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"xmg://"];
if (range.location != NSNotFound) {
NSString *method = [str substringFromIndex:range.location + range.length];
NSLog(@"%@", method);
// SEL sel = NSSelectorFromString(method);
// [self performSelector:sel];
}
// NSLog(@"%@", str);
return YES;
}
@end