[iOS基础控件 - 7.0] UIWebView

时间:2021-06-17 14:29:43
A.基本使用
1.概念
iOS内置的浏览器控件
Safari浏览器就是通过UIWebView实现的
 
2.用途:制作简易浏览器
(1)基本请求
创建请求
加载请求
(2)代理监听webView加载,例如提示正在加载
(3)前进、后退
使用NavigationController,在底部加上导航栏(勾选showToolBar),加上回退和前进按钮
(4)给顶部导航栏加上地址栏
UISearchBar
代理监听“搜索”按钮,提取地址栏文本
 
[iOS基础控件 - 7.0] UIWebView
 
 
 
[iOS基础控件 - 7.0] UIWebView
 
 //
// ViewController.m
// UIWebViewdDemo
//
// Created by hellovoidworld on 15/1/30.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UISearchBarDelegate, UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardButton;
- (IBAction)back:(UIBarButtonItem *)sender;
- (IBAction)forward:(UIBarButtonItem *)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 创建一个搜索栏
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(, , , );
searchBar.delegate = self; self.navigationItem.titleView = searchBar; self.webView.delegate = self;
} - (IBAction)back:(UIBarButtonItem *)sender {
NSLog(@"后退");
[self.webView goBack];
} - (IBAction)forward:(UIBarButtonItem *)sender {
NSLog(@"前进");
[self.webView goForward];
} #pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"开始加载");
} - (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"完成加载"); // 设置后退、前进按钮
if ([self.webView canGoBack]) {
self.backButton.enabled = YES;
} else {
self.backButton.enabled = NO;
} if ([self.webView canGoForward]) {
self.forwardButton.enabled = YES;
} else {
self.forwardButton.enabled = NO;
}
} #pragma mark - UISearchBarDelegate
/** 点击搜索按钮(虚拟键盘上) */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *str = searchBar.text;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", str]];
NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request];
} @end
 
3.加载本地资源
html、txt、doc、ppt、MP4、pdf
直接请求即可
 
4.自动识别电话、邮箱、地址
    // 识别所有类型的数据
    self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
 
B.其他用途
1.OC执行javascript
[iOS基础控件 - 7.0] UIWebView
 
 /** 运行js代码 */
- (IBAction)runJs {
NSString *str = @"alert('hello, world!');";
[self.webView stringByEvaluatingJavaScriptFromString:str];
}
 
 
2.javascript调用OC
在javascript中发送网页请求,使用OC截取,根据内容判断调用相应方法
 
这里使用js代码呼出相册选择:
[iOS基础控件 - 7.0] UIWebView
 
网页 & js 代码:
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运行OC代码</title>
</head>
<body> <script type="text/javascript">
function runOC() {
window.location.href="ios://openAlbum";
}
</script> <input type="button" value="运行OC代码" onclick="runOC();" /> </body>
</html>
 
OC代码:
 /** webView发送请求之前都会调用,可以拦截请求,选择加载(返回YES)还是不加载(返回NO)*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // 拦截请求
NSString *urlStr = request.URL.absoluteString;
NSRange range = [urlStr rangeOfString:@"ios://"]; // 如果是ios请求
if (range.length != ) {
// 方法名
NSString *method = [urlStr substringFromIndex:range.location + range.length]; // 包装SEL
SEL selector = NSSelectorFromString(method); // 执行方法
[self performSelector:selector withObject:nil];
} return YES;
} /** 打开相册 */
- (void) openAlbum {
// 创建一个相片选择控制器
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
[imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; // 使用相册来源 [self presentViewController:imagePC animated:YES completion:^{
NSLog(@"呼出相册");
}];
}