[iOS基础控件-7.0]UIWebView

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

A.基本使用

1.概念

iOS内置的浏览器控件

Safari浏览器就是通过UIWebView实现的

2.用途:制作简易浏览器

(1)基本请求

创建请求

加载请求

(2)代理监听webView加载,例如提示正在加载

(3)前进、后退

使用NavigationController,在底部加上导航栏(勾选showToolBar),加上回退和前进按钮

(4)给顶部导航栏加上地址栏

UISearchBar

代理监听“搜索”按钮,提取地址栏文本

1//2//ViewController.m3//UIWebViewdDemo4//5//Created by hellovoidworld on 15/1/30.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"ViewController.h"1011@interfaceViewController () <UISearchBarDelegate, UIWebViewDelegate>1213@PRoperty (weak, nonatomic) IBOutlet UIWebView *webView;1415@property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;16@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardButton;17- (IBAction)back:(UIBarButtonItem *)sender;18- (IBAction)forward:(UIBarButtonItem *)sender;1920@end2122@implementationViewController2324- (void)viewDidLoad {25[super viewDidLoad];26//Do any additional setup after loading the view, typically from a nib.2728//创建一个搜索栏29UISearchBar *searchBar =[[UISearchBar alloc] init];30searchBar.frame = CGRectMake(0,0,300,40);31searchBar.delegate=self;3233self.navigationItem.titleView =searchBar;3435self.webView.delegate=self;36}37383940- (IBAction)back:(UIBarButtonItem *)sender {41NSLog(@"后退");42[self.webView goBack];43}4445- (IBAction)forward:(UIBarButtonItem *)sender {46NSLog(@"前进");47[self.webView goForward];48}4950#pragmamark - UIWebViewDelegate51- (void)webViewDidStartLoad:(UIWebView *)webView {52NSLog(@"开始加载");53}5455- (void)webViewDidFinishLoad:(UIWebView *)webView {56NSLog(@"完成加载");5758//设置后退、前进按钮59if([self.webView canGoBack]) {60self.backButton.enabled =YES;61}else{62self.backButton.enabled =NO;63}6465if([self.webView canGoForward]) {66self.forwardButton.enabled =YES;67}else{68self.forwardButton.enabled =NO;69}70}7172#pragmamark - UISearchBarDelegate73/** 点击搜索按钮(虚拟键盘上)*/74- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {75NSString *str =searchBar.text;76NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", str]];77NSURLRequest *request =[NSURLRequest requestWithURL:url];7879[self.webView loadRequest:request];80}8182@end

3.加载本地资源

html、txt、doc、PPT、MP4、pdf

直接请求即可

4.自动识别电话、邮箱、地址

// 识别所有类型的数据

self.webView.dataDetectorTypes = UIDataDetectorTypeAll;

B.其他用途

1.OC执行javascript

1/** 运行js代码*/2-(IBAction)runJs {3NSString *str =@"alert('hello, world!');";4[self.webView stringByEvaluatingJavascriptFromString:str];5}

2.javascript调用OC

在javascript中发送网页请求,使用OC截取,根据内容判断调用相应方法

这里使用js代码呼出相册选择:

网页 & js 代码:

1<%@ page language="java"contentType="text/html; charset=UTF-8"2pageEncoding="UTF-8"%>3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">4<html>5<head>6<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">7<title>运行OC代码</title>8</head>9<body>1011<scripttype="text/javascript">12functionrunOC() {13window.location.href="ios://openAlbum";14}15</script>1617<inputtype="button"value="运行OC代码"onclick="runOC();"/>1819</body>20</html>

OC代码:

1/** webView发送请求之前都会调用,可以拦截请求,选择加载(返回YES)还是不加载(返回NO)*/2- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {34//拦截请求5NSString *urlStr =request.URL.absoluteString;6NSRange range = [urlStr rangeOfString:@"ios://"];78//如果是ios请求9if(range.length !=0) {10//方法名11NSString *method = [urlStr substringFromIndex:range.location +range.length];1213//包装SEL14SEL selector =NSSelectorFromString(method);1516//执行方法17[self performSelector:selector withObject:nil];18}1920returnYES;21}2223/** 打开相册*/24- (void) openAlbum {25//创建一个相片选择控制器26UIImagePickerController *imagePC =[[UIImagePickerController alloc] init];27[imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];//使用相册来源2829[self presentViewController:imagePC animated:YES completion:^{30NSLog(@"呼出相册");31}];32}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航