1. 加載
WebView可直接加載.mp4,.pdf,.html,.doc等格式文件。
@interface ViewController ()
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation ViewController
- (void)loadView {
self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.webView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.backgroundColor = [UIColor whiteColor];
// 加載網(wǎng)頁
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
// 加載doc
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"面向?qū)ο驝.doc" withExtension:nil];
// 加載pdf
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"史提夫喬布斯傳.pdf" withExtension:nil];
// 播放視頻
NSURL *url = [[NSBundle mainBundle] URLForResource:@"qixi.mp4" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
// 自動(dòng)檢測電話號(hào)碼、網(wǎng)址、郵件地址
self.webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
// 縮放網(wǎng)頁
self.webView.scalesPageToFit = YES;
}
@end
2. 與JS交互
1). OC調(diào)用JS代碼
@interface ViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation ViewController
- (void)loadView {
self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.webView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.backgroundColor = [UIColor whiteColor];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.scalesPageToFit = YES;
// 設(shè)置代理
self.webView.delegate = self;
}
#pragma mark 代理方法
// 等待網(wǎng)頁加載完成 才能執(zhí)行js
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 調(diào)用網(wǎng)頁中的函數(shù)
NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"test();"];
NSLog(@"----%@", string);
}
index.html
<!DOCTYPE html>
<html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
function test() {
return "abc";
}
</script>
</head>
<body>
<br /> <br /><br />
11111111111
<br />abc
<br />
<a href="source:///showMessage:/helloworld">HelloWorld!</a>
<br />
<a >baidu</a>
<br />
123@abc.com
<br />
http://www.baidu.com
<div id="container">
</div>
</body>
</html>
2). JS調(diào)用OC代碼
@interface ViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation ViewController
- (void)loadView {
self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.webView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.backgroundColor = [UIColor whiteColor];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.scalesPageToFit = YES;
// 設(shè)置代理
self.webView.delegate = self;
}
#pragma mark 代理方法
// 發(fā)送請求之前, JS調(diào)用OC代碼
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// 獲取url中的協(xié)議
NSLog(@"%@", request.URL.scheme);
// 判斷協(xié)議是否是自定義協(xié)議
if ([request.URL.scheme isEqualToString:@"source"]) {
NSLog(@"%@", request.URL.pathComponents);
// 獲取方法名
NSString *methodName = request.URL.pathComponents[1];
// 獲取參數(shù)
NSString *param = request.URL.pathComponents[2];
// 調(diào)用方法
// 把字符串的方法名稱轉(zhuǎn)換為一個(gè)selector
SEL method = NSSelectorFromString(methodName);
if ([self respondsToSelector:method]) {
// 解決執(zhí)行方法的內(nèi)存泄漏問題
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
// 調(diào)用放法
[self performSelector: method withObject:param];
#pragma mark diagnostic pop
}
}
// 返回NO,所有的請求都不執(zhí)行
return YES;
}
// 顯示對話框
- (void)showMessage:(NSString *)str {
UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
// 彈出的按鈕
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"clicker");
}];
[vc addAction:action];
[self presentViewController:vc animated:YES completion:nil];
}
@end