UIWebView是iOS sdk中一个最常用的组件。可以实现一个内置的浏览器组件(类似于Safari),可以通过它来浏览网页、打开文档等。可以直接将这个浏览器内嵌到应用的仁义位置。系统自带的Safari浏览器就是通过UIWebView实现的。
UIWebView直接继承了UIView基类,一般不可以与用户交互。
UIWebView主要有下面几个委托方法:
1、- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。
例子一——装载显示指定的HTML语句
//
// ViewController.m
// UIWebViewDemo
//
// Created by Apple on 16/5/18.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 可变字符
NSMutableString* html = [[NSMutableString alloc] init];
// 拼接一段HTML代码
[html appendString:@"<html>"];
[html appendString:@"<head>"];
[html appendString:@"<title> 欢迎您 </title>"];
[html appendString:@"</head>"];
[html appendString:@"<body>"];
[html appendString:@"<h2> 欢迎您访问<a href=\"http://www.sysu.edu.cn/2012/cn/index.htm\">"];
[html appendString:@"中山大学</a></h2>"];
// HTML代码中支持JavaScript脚本
[html appendString:@"<script language='javascript'>"];
[html appendString:@"alert('您正在使用的是UIWebView');</script>"];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
// 屏幕大小
CGRect rect = self.view.frame;
// 创建一个UIWebView组件(UIWebview就是一个iOS中内嵌的浏览器)
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, rect.size.width, rect.size.height)];
// 加载、并显示HTML代码,UIWebView完全支持包含JavaScript脚本的字符串
// 将本地html文件内容嵌入webView
[webView loadHTMLString:html
baseURL:[NSURL URLWithString:@"http://www.sysu.edu.cn/2012/cn/index.htm"]];
// 添加控件
[self.view addSubview:webView];
}
@end
但这个例子会出现点击“中山大学”这个链接时没反应的问题,因为出现了如下问题:
可以参照我的这篇博文进行修改:
效果图如下:
例子二——获取网络资源
//
// ViewController.m
// UIWebViewApp
//
// Created by Apple on 16/5/18.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
UIActivityIndicatorView* progress;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect rect = self.view.frame;
// 创建一个UIWebView组件(UIWebview就是一个iOS中内嵌的浏览器)
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, rect.size.width, rect.size.height)];
// 创建一个请求url
NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];
// 创建一个url请求对象
NSURLRequest* request = [NSURLRequest requestWithURL:url];
// 通知UIWebView加载url表示的请求资源
// 直接给出url地址即可将web content载入。
[webView loadRequest:request];
// 设置用户交互操作支持
[webView setUserInteractionEnabled:YES];
// 设置代理
webView.delegate = self;
/**
// 返回
[webView goBack];
// 向前
[webView goForward];
// 重新加载数据
[webView reload];
// 停止加载数据
[webView stopLoading];
*/
[self.view addSubview:webView];
}
// UIWebView开始加载时执行
- (void)webViewDidStartLoad:(UIWebView *)webView{
// 如果网页中还附带了其他请求,则该方法会执行多次
NSLog(@"---------- webViewDidStartLoad------[%@]------", webView.request.URL.path);
//创建不确定进度条
progress = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
//设置进度条背景
[progress setBackgroundColor:[UIColor blackColor]];
//设置进度条风格
[progress setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:progress];
// 启动进度条
[progress startAnimating];
}
// UIWebView完成加载时执行
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"----------webViewDidFinishLoad------------");
// 停止进度条
[progress stopAnimating];
}
// UIWebView加载失败时执行(关闭wifi测试)
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"----------didFailLoadWithError----------[%@]-----",[error localizedDescription]);
}
@end
如果出现以下错误,可以参考我的这篇博文进行修改:
效果图如下:
例子三——自定义输入框请求网络资源
//
// ViewController.m
// UIWebViewSendApp
//
// Created by Apple on 16/5/18.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
UITextField* urlTextField;
UIWebView* webView;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGSize size = self.view.frame.size;
// 创建一个UIWebView组件(UIWebview就是一个iOS中内嵌的浏览器)
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, size.width, size.height)];
// 设置用户交互操作支持
[webView setUserInteractionEnabled:YES];
// 设置代理
webView.delegate = self;
// 创建一个UITextField,接受输入
urlTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 30, 280, 20)];
[urlTextField setBorderStyle:UITextBorderStyleRoundedRect];
//设置默认文本
[urlTextField setText:@"http://www.baidu.com"];
// 创建一个UIButton,点击发送请求
UIButton *go = [UIButton buttonWithType:UIButtonTypeSystem];
[go setFrame:CGRectMake(280, 30, 50, 20)];
[go setTitle:@"GO" forState:UIControlStateNormal];
// 绑定requestGo:方法
[go addTarget:self action:@selector(requestGo:) forControlEvents:UIControlEventTouchUpInside];
// 添加控件到视图
[self.view addSubview:urlTextField];
[self.view addSubview:go];
[self.view addSubview:webView];
}
-(void)requestGo:(id*)sender{
// 创建一个请求url
NSURL * url = [NSURL URLWithString:[urlTextField text]];
// 创建一个url请求对象
NSURLRequest *req = [NSURLRequest requestWithURL:url];
// 通知UIWebView加载url表示的请求资源
[webView loadRequest:req];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
UIAlertView *errAlert = [[UIAlertView alloc] initWithTitle:@"请求失败"
message:[error localizedDescription]
delegate:nil cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[self.view addSubview:errAlert];
}
@end
如果出现以下错误,可以参考我的这篇博文进行修改:
效果图如下: